以下 Java 代码将数据 POST 到网站,然后作为响应进行重定向(状态 302)。它在我的 PC(Eclipse、Java、Ubuntu)上完美运行,完全符合我的要求。
我尝试了一切来发布代码功能,但我就是做不到。
Java代码:
// Preparing the CLIENT and POST Method
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://na.leagueoflegends.com/ladders/solo-5x5");
try {
// Add your POST METHOD attributes
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("op", "Search"));
nameValuePairs.add(new BasicNameValuePair("player", "Jaiybe"));
nameValuePairs.add(new BasicNameValuePair("ladder_id", "3"));
nameValuePairs.add(new BasicNameValuePair("form_build_id",
"form-526370b788622996caa3669e7b975ccf"));
nameValuePairs.add(new BasicNameValuePair("form_id",
"ladders_filter_form"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
// RESPONE THAT WORKS WITH JAVA
System.out.println("Location:");
String LocationHeader = response.getFirstHeader("location").getValue();
System.out.println(LocationHeader);
System.out.println();
// To get the BODY I would have to parse that again - since its not REDIRECTING automatically
HttpClient httpclient2 = new DefaultHttpClient();
HttpPost httppost2 = new HttpPost(LocationHeader);
response = httpclient2.execute(httppost2);
System.out.println("And EVEN the response body:");
System.out.println(EntityUtils.toString(response.getEntity()));
代码的作用是:
- Posts
- 获取重定向 - 获取位置标头
- 解析位置
我需要 android 来做同样的事情。 “位置”或响应正文都可以,我不需要两者。
帖子:http://www.anddev.org/networking-database-problems-f29/httppost-clientprotocolexception-t56118.html
我已经找到问题了!
httpclient.getParams().setParameter("http.protocol.version",
HttpVersion.HTTP_1_0);
只需更改这一行 - 版本 1_0 有效,而 1_1 无效。不要问我为什么:)
谢谢你们!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)