【问题标题】:httpclient responsehttpclient响应
【发布时间】:2012-02-07 21:26:56
【问题描述】:

我正在尝试自动登录网页。我假设我通过了正确的凭据。

entity.getContentLength() 显示 20 但我看到的响应格式不正确。它不是 HTML。我应该如何进一步进行。下面是我的代码。

String input_text = "https://www.abc.com";
                HttpPost httpost = new HttpPost(input_text);

                List <NameValuePair> nvps = new ArrayList <NameValuePair>();
                nvps.add(new BasicNameValuePair("email", "abc@xyz.com"));
                nvps.add(new BasicNameValuePair("passsword", "ttyyeri"));   
                nvps.add(new BasicNameValuePair("publicLoginToken",""));

                httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

                HttpResponse  response = httpclient.execute(httpost);
                entity  = response.getEntity();

                if (entity != null) {
                  BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent()));
                  String readLine;
                  while(((readLine = br.readLine()) != null)) {
                    System.err.println("br   :"+readLine);
                 }
                  System.out.println("Response content length: " + entity.getContentLength());

                }      

                System.out.println("HTML Content :::"+entity.getContent().toString());

【问题讨论】:

  • It is not an HTML 比它是什么。?
  • 您可以发布您收到的回复吗?
  • @ranRag..repsonse 不是 HTML 格式。以上 System.err.println("br :"+readLine);打印未格式化的东西。我无法复制粘贴回复...看起来像这样...br :‹.
  • @javaiText : http 302 表示重定向。你需要设置一个http referer。使用firefox's live http header插件查看手动登录时实际发生的情况。
  • 如何设置http referer?当我监控实时 HttpHeader 时,上面的 v3/credential url 有Referer:hautelook.com/login。这是什么意思?有什么可能有帮助的例子吗?

标签: java httpclient autologin


【解决方案1】:

试试

  StatusLine l = response.getStatusLine();
  System.out.println(l.getStatusCode() + "  " + l .getReasonPhrase());

输出?

【讨论】:

  • httpGet 为我提供了“新会员登录页面”的 HTML 响应
  • a 查看了页面 - 他们正在以 JSON 格式发送数据 {"email":"sdasd","password":"asdas","meta":{"screen_resolution":{"height ":768,"width":1366}}}
  • 所以我需要创建一个 JASON 对象并以这种方式传递凭据?a
【解决方案2】:

听起来您正在获得授权请求重定向。这可能已经在这里介绍过:Http Basic Authentication in Java using HttpClient?

【讨论】:

    【解决方案3】:

    查看HttpResponse Header,可以找到内容类型和响应码。 这将帮助您找到问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-08
      • 1970-01-01
      • 1970-01-01
      • 2016-12-20
      • 2014-03-08
      • 2014-08-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多