简体   繁体   English

Java:从HTTP服务器上的套接字读取POST数据

[英]Java: Read POST data from a socket on an HTTP server

I have a website (python/django) that needs to use a load of Java resources that may or may not be on the same server. 我有一个网站(python / django)需要使用可能或可能不在同一服务器上的Java资源。 Therefore I am writing a mini webserver in Java that will receive a request and then when processing is finished, POST some data back to a url on the site. 因此,我正在用Java编写一个迷你网络服务器来接收请求,然后在处理完成后,将一些数据发送回网站上的网址。

I have got the java code receiving connections on sockets and responding with some simple HTML. 我有java代码在套接字上接收连接并使用一些简单的HTML进行响应。

My problem is that I will POST data to the Java server and that code needs to act on the data. 我的问题是我将POST数据发送到Java服务器,并且该代码需要对数据进行操作。 How do I go about reading the data that is posted in the HTML request, if it is even possible. 如果可能的话,我如何阅读HTML请求中发布的数据。 If not, is there any other way you would do this. 如果没有,还有其他方法可以做到这一点。

If you think I am going about this in completely the wrong way then please tell me and I will consider another method, but after conversing with some Java developers, this seemed like the best way for what I was doing. 如果你认为我是以完全错误的方式解决这个问题,那么请告诉我,我会考虑另一种方法,但在与一些Java开发人员交谈之后,这似乎是我正在做的最好的方式。

Thanks 谢谢

You probably don't need to write a http server yourself, just use some lightweight java web server/servlet container like jetty or simple 您可能不需要自己编写http服务器,只需使用一些轻量级的Java Web服务器/ servlet容器,如jettysimple

and looks here if you still want to know how to parse a http POST request http://www.jmarshall.com/easy/http/#postmethod 如果您仍想知道如何解析http POST请求, 查看此处http://www.jmarshall.com/easy/http/#postmethod

In your case I probably wouldn't work with low level socket connections. 在你的情况下,我可能不会使用低级套接字连接。

I recommend you to use a servlet container with some sort of webservice or maybe only a simple servlet depending on your needs. 我建议你使用一个带有某种web服务的servlet容器,或者根据你的需要使用一个简单的servlet。 With a servlet you can easily access the POST data, process it and return something to the caller. 使用servlet,您可以轻松访问POST数据,处理它并将某些内容返回给调用者。

If you don't want to use a stand alone servlet container like Tomcat you can try an embeddable servlet container like Jetty or winstone servlet container. 如果您不想使用像Tomcat这样的独立servlet容器,可以尝试使用可嵌入的servlet容器,如Jetty或winstone servlet容器。

If you need something more sophisticated you can use some webservice technology like JAX-RS or JAX-WS. 如果您需要更复杂的东西,可以使用一些Web服务技术,如JAX-RS或JAX-WS。 This allows you to provide a fully featured API for your Java resources in an easy way. 这使您可以轻松地为Java资源提供功能齐全的API。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM