HttpPost - >重定向 - >位置的反应或身体需要 [英] HttpPost -> Redirect -> Location or body of response needed
问题描述
下面是Java的code的员额数据到一个网站,比被重定向作为响应(状态302)。它完美我的电脑(Eclipse中的Java,Ubuntu的)上,这不正是我想要做的事。
Here is Java code that POSTs data to a website and than gets redirected as a response (status 302). It works perfectly on my PC (Eclipse, Java, Ubuntu), it does exactly what I want it to do.
我试过的所有内容张贴code的功能,但我只是我不能。
I tried quite everything to post the code functionality but I just am not able to.
Java的code:
Java code:
// 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()));
code的作用:
Code does:
- 文章
- 被重定向 - 获取位置的标题
- 解析的位置
和我需要的Android做的一样。无论是位置或repsonse的身体,是确定的,我不需要两个。
And I need android to do the same. Either "Location" or body of repsonse, is ok, I dont need both.
该职位:<一href="http://www.anddev.org/networking-database-problems-f29/httppost-clientprotocolexception-t56118.html" rel="nofollow">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没有。不要问我为什么:)
Just changing this one line - version 1_0 works and 1_1 does not. Don't ask me why :)
感谢大家!
这篇关于HttpPost - &GT;重定向 - &GT;位置的反应或身体需要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!