android - HttpClient VS Jsoup : Http package header look the same but not the response is difference?

标签 android http network-programming httpclient jsoup

如下图是从 Wireshark 抓取的,左边是通过这段代码从 Jsoup 创建的包。

Connection.Response response = Jsoup.connect("http://pantip.com/login/authentication")
            .header("Content-Type", "application/x-www-form-urlencoded")
            .timeout(9000)
            .method(Connection.Method.POST)
            .data("member[email]", username, "member[crypted_password]", password, "action", "login", "redirect", "")
            .followRedirects(false)
            .execute();

右侧是通过以下代码从 HttpClient 创建的包。

 List<NameValuePair> nvps = new ArrayList<NameValuePair>();
    nvps.add(new BasicNameValuePair("member[email]", username));
    nvps.add(new BasicNameValuePair("member[crypted_password]", password));
    nvps.add(new BasicNameValuePair("action", "login"));
    nvps.add(new BasicNameValuePair("redirect", ""));

    HttpPost postLogin = new HttpPost("http://pantip.com/login/authentication");

    postLogin.setEntity(new StringEntity("member[email]="+username+"&member[crypted_password]="+password+"&action=login&redirect="));
    postLogin.addHeader("Content-Type", "application/x-www-form-urlencoded");
    postLogin.addHeader("Accept-Encoding", "gzip");
    postLogin.addHeader("User-Agent", "Dalvik/1.4.0 (Linux; U; Android 2.3.7; Full Android on x86 Emulator Build/GINGERBREAD)");

我尝试为每个 header 设置相同的值,包括用户代理。这是用于登录网站的包。

我的问题是由 Jsoup 创建的左侧包正在工作,但是当我使用 HttpClient 时,服务器通过登录响应不正确但请求包是相同的,为什么?

在我的 android 项目中,我必须使用 HttpClient。这是我的约束。

Package Compare

最佳答案

您可能必须在 StringEntity 对象中设置内容类型。另外,为什么要制作 BasicNameValuePairs 然后只使用字符串?

关于android - HttpClient VS Jsoup : Http package header look the same but not the response is difference?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17746067/

相关文章:

java - 在 AltBeacon 库中,我看到 iBeacons 每秒刷新多次的原因是什么?

android - Adb emu 命令有时不起作用

android - 当 FAB 重力处于底部时,Circular Reveal 不起作用

javascript - HTTP 文件下载 : Monitoring Download Progress

android - Widget.AppCompat.Button colorButtonNormal 显示灰色

php - 一些HTTP响应的第一行变成 “undefined undefined undefined”

performance - Content-Length header 与分块编码

r - 在 stplanr 中向 SpatialLinesNetwork 添加新节点

Java UDP 数据报包 - 切片数据以适应缓冲区大小

sockets - UDP 组播服务器可以将数据包发送到 LAN 之外吗?