为什么在将数据发布到表单时 HttpClient 不起作用,而 HttpUrlConnenction 起作用

发布于 2024-08-18 11:20:13 字数 3603 浏览 3 评论 0原文

我正在构建一个 Android 应用程序,它应该在我的网站上执行 GET 以获取两个 cookie,然后使用这些 cookie 向同一网站执行发布操作。

如前所述,我从 GET 开始,并使用 org.apache.http.client.HttpClient 来执行此操作。

String requiredCookies = "";
HttpContext localContext = null;

System.out.println("------------------GET----------------------");
HttpClient httpClient = new DefaultHttpClient();
HttpGet get = new HttpGet("www.mysitegeturl.com");

//Creating a local instance of cookie store.
CookieStore cookieJar = new BasicCookieStore();

// Creating a local HTTP context
localContext = new BasicHttpContext();

// Bind custom cookie store to the local context
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieJar);

HttpResponse response;
try {
    response = httpClient.execute(get, localContext);
HttpEntity entity = response.getEntity();
System.out.println(response.getStatusLine());
if (entity != null) {
   System.out.println("Response content length: " + entity.getContentLength());
}

//Do this so that Java.net impl should work
List<Cookie> cookies = cookieJar.getCookies();
for (int i = 0; i < cookies.size(); i++) {
     requiredCookies += cookies.get(i).getName()+"="+cookies.get(i).getValue()+";";
}

if (entity != null) {
    entity.consumeContent();
}

} catch (ClientProtocolException e1) {
        // TODO Auto-generated catch block
 e1.printStackTrace();
} catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}
System.out.println("------------------GET-END---------------------");

到目前为止,一切都很好。暂时不要介意 requiredCookies 行,它将在 Java.net impl 中使用,因为我无法让 HttpClient 工作 =(. 让我们看一下不工作的 HttpClient Post 部分。

System.out.println("------------------HttpClient - POST----------------------");
HttpPost post = new HttpPost("www.mysiteposturl.com");

//Params       
HttpParams params = new BasicHttpParams();

params.setParameter("foo", "post");
params.setParameter("bar", "90");
params.setParameter("action", "search");

post.setParams(params);
post.setHeader("Content-Type", "application/x-www-form-urlencoded");

try {
    HttpResponse response2 = httpClient.execute(post, localContext);
     System.out.println(response2.getStatusLine());
} catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
}
System.out.println("------------------POST END---------------------");

现在发生的情况是,我使用存储 cookie 的 localContext 执行 POST。这是行不通的。我收到 HTTP/1.1 401 无会话。因为我没有运气,所以我尝试了另一种方法(java.net.HttpURLConnection)。请记住,我仍然使用相同的 GET 部分

URL url = new URL("www.mysiteposturl");
HttpURLConnection connection = null;

String dataString = "bar=90&foo=post&action=search";

try {
connection = (HttpURLConnection)url.openConnection();
connection.setRequestProperty("Cookie", requiredCookies);
//Set to POST
connection.setDoOutput(true);
Writer writer = new OutputStreamWriter(connection.getOutputStream());
writer.write(dataString);
writer.flush();
writer.close();
connection.connect();

if (connection.getResponseCode() == 200 || connection.getResponseCode() == 201) {
    System.out.println(connection.getContent().toString());
} else {
   System.out.println("Error");
}
} catch (IOException e) {
   // TODO Auto-generated catch block
     e.printStackTrace();
} catch (MalformedURLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
}

System.out.println("------------------POST END---------------------");

,并且显示 VIOLA a 200,一切都像魅力一样。你们觉得怎么样?有人可以给我一个答案吗,因为我无法弄清楚。

I'm building an android app which should perform a GET on my site to get two cookies and then perform a post to the same site with these cookies.

As mentioned I start of with the GET and I'm using org.apache.http.client.HttpClient to perform this operation.

String requiredCookies = "";
HttpContext localContext = null;

System.out.println("------------------GET----------------------");
HttpClient httpClient = new DefaultHttpClient();
HttpGet get = new HttpGet("www.mysitegeturl.com");

//Creating a local instance of cookie store.
CookieStore cookieJar = new BasicCookieStore();

// Creating a local HTTP context
localContext = new BasicHttpContext();

// Bind custom cookie store to the local context
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieJar);

HttpResponse response;
try {
    response = httpClient.execute(get, localContext);
HttpEntity entity = response.getEntity();
System.out.println(response.getStatusLine());
if (entity != null) {
   System.out.println("Response content length: " + entity.getContentLength());
}

//Do this so that Java.net impl should work
List<Cookie> cookies = cookieJar.getCookies();
for (int i = 0; i < cookies.size(); i++) {
     requiredCookies += cookies.get(i).getName()+"="+cookies.get(i).getValue()+";";
}

if (entity != null) {
    entity.consumeContent();
}

} catch (ClientProtocolException e1) {
        // TODO Auto-generated catch block
 e1.printStackTrace();
} catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}
System.out.println("------------------GET-END---------------------");

So far so good. Don't mind the requiredCookies line yet, it will be used in the Java.net impl since I can't get the HttpClient one to work =(.
Let's take a look at the non working HttpClient Post part.

System.out.println("------------------HttpClient - POST----------------------");
HttpPost post = new HttpPost("www.mysiteposturl.com");

//Params       
HttpParams params = new BasicHttpParams();

params.setParameter("foo", "post");
params.setParameter("bar", "90");
params.setParameter("action", "search");

post.setParams(params);
post.setHeader("Content-Type", "application/x-www-form-urlencoded");

try {
    HttpResponse response2 = httpClient.execute(post, localContext);
     System.out.println(response2.getStatusLine());
} catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
}
System.out.println("------------------POST END---------------------");

What happens now is that I perform a POST with the localContext where the cookies are stored. This doesn't work. I get a HTTP/1.1 401 No session. Since I had no luck with this I tried another approach(java.net.HttpURLConnection). Remember I still use the same GET part

URL url = new URL("www.mysiteposturl");
HttpURLConnection connection = null;

String dataString = "bar=90&foo=post&action=search";

try {
connection = (HttpURLConnection)url.openConnection();
connection.setRequestProperty("Cookie", requiredCookies);
//Set to POST
connection.setDoOutput(true);
Writer writer = new OutputStreamWriter(connection.getOutputStream());
writer.write(dataString);
writer.flush();
writer.close();
connection.connect();

if (connection.getResponseCode() == 200 || connection.getResponseCode() == 201) {
    System.out.println(connection.getContent().toString());
} else {
   System.out.println("Error");
}
} catch (IOException e) {
   // TODO Auto-generated catch block
     e.printStackTrace();
} catch (MalformedURLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
}

System.out.println("------------------POST END---------------------");

And VIOLA a 200 is displayed and everything works like a charm. What do you guys think? Could someone please provide me with an answer because I can't figure it out.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧人九事 2024-08-25 11:20:13

问题似乎是您的设置中有两个不同的主机名。这将导致 HTTP 客户端不发送不同主机的 cookie。您可以尝试更改 cookie 存储中 cookie 的域,或使用同一主机进行 GET 和 POST。此外,您可以像在 HttpURLConnection 示例中那样手动将 cookie 添加到 HTTP 客户端中的标头中。

The problem appears to be that you have two different host names in the setup. This will cause HTTP Client to not send cookies for a different host. You could try changing the domain of the cookies in the cookie store, or using the same host for GET and POST. Additionally you could manually add the cookies to the headers in HTTP Client as you did in the HttpURLConnection example.

花期渐远 2024-08-25 11:20:13

我想您为两个请求使用了两个完全不同的域是一个错误 - 即您试图掩盖您的真实 URL?如果没有,那就是您没有收到任何 cookie 的原因。如果我们只是想屏蔽您的网址,那么这就是 example.com 存在的原因。 :)

或者,这完全是我上周写的代码中的一个想法——它在多个 GET、POST 和子域中运行良好:

DefaultHttpClient httpClient = new DefaultHttpClient();
CookieStore cookieJar = new BasicCookieStore();
httpClient.setCookieStore(cookieJar);

即我明确使用了 DefaultHttpClient,我使用它相信 cookie 存储有那些额外的 get/setter。我认为我也没有使用任何上下文对象。

I guess it was a mistake that you used two completely different domains for your two requests — i.e. you were trying to mask your real URL? If not, then that's why you're not getting any cookies. If were just trying to mask your URL, well that's why example.com exists. :)

Alternatively, and this is completely off the top of my head from code I wrote last week — it worked fine across multiple GETs, POSTs and subdomains:

DefaultHttpClient httpClient = new DefaultHttpClient();
CookieStore cookieJar = new BasicCookieStore();
httpClient.setCookieStore(cookieJar);

i.e. I'm explicitly using a DefaultHttpClient, which I believe has those extra get/setters for the cookie store. I don't think I used any context objects either.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文