发送POST请求到本地主机 [英] Sending a post request to localhost
问题描述
我需要从我的Android设备发送一个HTTP POST请求到在我的本地主机上运行的Web服务器:8080,是由谷歌的应用引擎主管
I need to send a http post request from my android device to a web server that is running on my localhost:8080 and is being hosted by the google app engine.
目前的code是这样的:
the current code is this:
try {
HttpPost httppost = new HttpPost("http://192.168.2.220:8080");
httppost.setHeader("Content-type", "application/json");
ResponseHandler <String> responseHandler = new BasicResponseHandler();
StringEntity se = new StringEntity(object.toString());
se.setContentEncoding(newBasicHeader(HTTP.CONTENT_TYPE,"application/json"));
httppost.setEntity(se);
System.out.println("Request Sent");
String response = httpclient.execute(httppost, responseHandler);
String temp = EntityUtils.toString(response.getEntity());
} catch (ClientProtocolException e) {}
catch (IOException e) {
}
}
我也试着设置:
HttpPost httppost = new HttpPost("http://10.0.2.2:8080");
在所有情况下,反应是空,程序力关闭。我是正确的发送请求?任何人都可以在这里指导我?
In all cases, the response is null and the program force closes. am i sending the request correctly? can anyone please guide me here?
感谢
推荐答案
发送到本地主机的请求是指将其发送到本地机器。你的情况这将是Android设备。你想将请求发送到您的桌面机,这是一个远程主机。问题是,默认情况下的AppEngine dev_sever只绑定到本地地址,所以它不能被远程访问(即从你的Android设备)。你需要通过 - 地址
选项,以使从外部访问。检查您的计算机的IP,并把它作为地址。是这样的:
'Send a request to localhost' means to send it to the local machine. In your case that would be the Android device. You want to send the request to your desktop machine, which is a remote host. The problem is that the Appengine dev_sever by default only binds to the local address, so it can't be accessed remotely (i.e., from your Android device). You need to pass the --address
option to make accessible from the outside. Check your computer's IP and pass it as the address. Something like:
dev_appserver.cmd --address=192.168.2.220
详情点击此处:<一href=\"http://$c$c.google.com/appengine/docs/java/tools/devserver.html\">http://$c$c.google.com/appengine/docs/java/tools/devserver.html
这篇关于发送POST请求到本地主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!