【问题标题】:HttpClient VS Jsoup : Http package header look the same but not the response is difference?HttpClient VS Jsoup:Http 包头看起来一样,但响应不同?
【发布时间】:2013-07-19 12:26:40
【问题描述】:

如下图从 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)");

我尝试将每个标头设置为包含用户代理的相同值。这是登录网站的包。

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

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

【问题讨论】:

    标签: android http network-programming httpclient jsoup


    【解决方案1】:

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

    【讨论】:

    • 哦,BasicNameValuePairs 是我的错误,我多次修改代码以使其正常工作。所以,我忘记了。谢谢你的建议。我会尽快尝试的。
    猜你喜欢
    • 2014-12-18
    • 2015-08-31
    • 1970-01-01
    • 2021-09-15
    • 2020-11-23
    • 2017-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多