[英]Server does not respond to concurrent requests in HTTP/1.1 Client using Java
[英]Java server to handle Client requests and respond to it?
我正在寻找创建Java Server来处理客户端请求并对其进行响应的概念,我想使用不允许Socket连接的Google App引擎,那么在这种情况下客户端和服务器是否可以使用Http请求进行通信? 如果有人可以向我阐明逻辑并提供几行代码,我将非常高兴。
谢谢
简单框架可能会提供您想要的东西。 它允许您以相对较少的开销将HTTP服务器嵌入到您的应用程序中:
import org.simpleframework.http.core.Container;
import org.simpleframework.transport.connect.Connection;
import org.simpleframework.transport.connect.SocketConnection;
import org.simpleframework.http.Response;
import org.simpleframework.http.Request;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.io.PrintStream;
public class HelloWorld implements Container {
public void handle(Request request, Response response) {
PrintStream body = response.getPrintStream();
long time = System.currentTimeMillis();
response.set("Content-Type", "text/plain");
response.set("Server", "HelloWorld/1.0 (Simple 4.0)");
response.setDate("Date", time);
response.setDate("Last-Modified", time);
body.println("Hello World");
body.close();
}
public static void main(String[] list) throws Exception {
Container container = new HelloWorld();
Connection connection = new SocketConnection(container);
SocketAddress address = new InetSocketAddress(8080);
connection.connect(address);
}
}
为了与其他解决方案进行比较,请注意,Simple不仅是可嵌入的,而且是开源的,完全自包含的并且始终是异步的。 祝好运!
感谢所有答案,但我需要一种简单的方法在Android App中使用它,如以下代码所示:
HTTP GET
`
try {
HttpClient client = new DefaultHttpClient();
String getURL = "http://www.google.com";
HttpGet get = new HttpGet(getURL);
HttpResponse responseGet = client.execute(get);
HttpEntity resEntityGet = responseGet.getEntity();
if (resEntityGet != null) {
//do something with the response
Log.i("GET RESPONSE",EntityUtils.toString(resEntityGet));
}
} catch (Exception e) {
e.printStackTrace();
}`
HTTP POST
try {
HttpClient client = new DefaultHttpClient();
String postURL = "http://somepostaddress.com";
HttpPost post = new HttpPost(postURL);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("user", "kris"));
params.add(new BasicNameValuePair("pass", "xyz"));
UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params,HTTP.UTF_8);
post.setEntity(ent);
HttpResponse responsePOST = client.execute(post);
HttpEntity resEntity = responsePOST.getEntity();
if (resEntity != null) {
Log.i("RESPONSE",EntityUtils.toString(resEntity));
}
} catch (Exception e) {
e.printStackTrace();
}
该代码来自此站点 ,您不需要任何其他Jar文件即可在Android中使用它,并且我将其与Google App引擎一起使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.