简体   繁体   English

为什么我收到 Http/1.1 400 错误请求?

[英]Why am I getting Http/1.1 400 Bad request?

Here is a snippet of my code.这是我的代码片段。 I am trying to login to the website using HTTPOST.我正在尝试使用 HTTPOST 登录该网站。

I keep getting "400 Bad Request" I have tried various combinations.我一直收到“400 Bad Request” 我尝试了各种组合。 I tried passing username/password as header instead of NameValuePair.我尝试将用户名/密码作为标题而不是 NameValuePair 传递。 But same result.但同样的结果。 Is there something I am missing?有什么我想念的吗?

HttpClient httpclient = new DefaultHttpClient();
        
HttpPost post = new HttpPost("https://identi.ca/main/login");
    

List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("nickname", u));
nvps.add(new BasicNameValuePair("password", p));
nvps.add(new BasicNameValuePair("submit", "Login"));

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

HttpResponse response = httpclient.execute(post);
HttpEntity entity = response.getEntity();

Log.i("Login form POST result: ", response.getStatusLine().toString());
        

Thanks for help.感谢帮助。

A HTTP service will send a 400 response if it thinks you have sent an invalid / incorrect / inappropriate.如果 HTTP 服务认为您发送了无效/不正确/不适当的信息,它将发送 400 响应。 There is no way to tell from your code, what the actual arguments sent will be, or (more important) what the server expects you to send.无法从您的代码中得知发送的实际参数是什么,或者(更重要的是)服务器希望您发送什么。

To diagnose this you will need to:要对此进行诊断,您需要:

  • find out what your code is actually sending, and找出您的代码实际发送的内容,以及
  • find out what the server is expecting you to send.找出服务器希望您发送的内容。

The second may involve:第二个可能涉及:

  • looking at the web page(s) that a normal user with a web browser uses to make the request,查看具有 Web 浏览器的普通用户用来发出请求的网页,
  • looking at the body of the 400 response you are getting from the server,查看您从服务器获得的 400 响应的正文,
  • looking at the server logs, and / or查看服务器日志,和/或
  • looking at the server's web API documentation (or source code).查看服务器的 Web API 文档(或源代码)。

In this case, it looks like there is an extra token parameter that needs to be supplied.在这种情况下,看起来需要提供额外的token参数。

However, I suspect that you might be taking the wrong approach entirely.但是,我怀疑您可能完全采取了错误的方法。 The "identi.ca" site uses StatusNet which has a couple of published APIs for clients. “identi.ca”站点使用 StatusNet,它为客户端提供了几个已发布的 API There's no mention of talking to the "login" URL that I could see.没有提到与我可以看到的“登录”URL 交谈。

Looking at the source for that login page, there is a hidden field called token that you probably have to return to the server.查看该登录页面的源代码,有一个名为token的隐藏字段,您可能必须将其返回给服务器。 You will need to GET the content of that page first, extract the token value, and include that in your POST response.您需要先获取该页面的内容,提取令牌值,并将其包含在您的 POST 响应中。

Though I know the right answer has been marked, wanted to give some vision to others passing by.虽然我知道正确答案已被标记,但还是想给路过的其他人一些愿景。 In my case, this is what caused the bad request就我而言,这就是导致错误请求的原因

form.add(new BasicNameValuePair("return_url", "https://myexample.example.com/#/providermanageger?vi={VERIFICATION_INTENT_ID}"));

The moment I removed the /#/providermanageger我删除/#/providermanageger的那一刻

It began to work.它开始工作了。 So keep doing a trial and error.所以继续试错。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM