http客户端响应

发布于 2025-01-03 14:28:16 字数 1398 浏览 2 评论 0原文

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

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", "[email protected]"));
                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());

I am trying to autologin into a webpage. Im asssuming that i pass the proper credentials.

entity.getContentLength() shows 20 but the repsonse i see is not well formatted. It is not an HTML. How should i proceed further. Below is my code.

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

                List <NameValuePair> nvps = new ArrayList <NameValuePair>();
                nvps.add(new BasicNameValuePair("email", "[email protected]"));
                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());

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

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

发布评论

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

评论(3

懷念過去 2025-01-10 14:28:16

尝试

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

输出?

try

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

output ?

一抹淡然 2025-01-10 14:28:16

听起来您正在收到授权请求重定向。这可能已经在这里介绍过:Java 中使用 HttpClient 进行 Http Basic 身份验证?

Sounds like you are getting an authorization request redirect. This may have already been covered here: Http Basic Authentication in Java using HttpClient?

森末i 2025-01-10 14:28:16

研究 HttpResponse 标头,您可以找到内容类型和响应代码。
这将帮助您找到问题。

Investigate the HttpResponse Header, you can find the content type and the response code.
Which will help you to find the problem.

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