java - HTTP/1.1 400 错误请求 Apache

标签 java apache http httpclient

我正在尝试使用我编写的以下代码登录 Twitter。问题是在每次执行时我都会收到 400 Bad Request 作为响应。我已经尝试了无数次尝试让它发挥作用,但没有成功。

public void login(String url) throws ClientProtocolException, IOException{
        HttpClient client = HttpClientBuilder.create().build();
        HttpGet request = new HttpGet(url);

        // add request header
        request.addHeader("User-Agent", USER_AGENT);
        HttpResponse response = client.execute(request);

        System.out.println("Response Code : " 
                    + response.getStatusLine().getStatusCode());

        BufferedReader rd = new BufferedReader(
            new InputStreamReader(response.getEntity().getContent()));

        StringBuffer result = new StringBuffer();
        String line = "";
        while ((line = rd.readLine()) != null) {
            result.append(line);
        }

        // set cookies
        setCookies(response.getFirstHeader("Set-Cookie") == null ? "" : response.getFirstHeader("Set-Cookie").toString());

        Document doc = Jsoup.parse(result.toString());
        System.out.println(doc);

        // Get input elements
        Elements loginform = doc.select("div.clearfix input[type=hidden][name=authenticity_token]");
        String auth_token = loginform.attr("value");
        System.out.println("Login: "+auth_token);

        List<NameValuePair> paramList = new ArrayList<NameValuePair>();
        paramList.add(new BasicNameValuePair("authenticity_token", auth_token));
        paramList.add(new BasicNameValuePair("session[username_or_email]", "twitter_username"));
        paramList.add(new BasicNameValuePair("session[password]", "twitter_password"));

        System.out.println(paramList);

        HttpPost post = new HttpPost(url);

        // add header
        post.setHeader("Host", "twitter.com");
        post.setHeader("User-Agent", USER_AGENT);
        post.setHeader("Accept", "text/html,application/xhtml;q=0.9,*/*;q=0.8");
        post.setHeader("Accept-Language", "en-US,en;q=0.5");
        post.setHeader("Keep-Alive", "115");
        post.setHeader("Cookie", getCookies());
        post.setHeader("Connection", "keep-alive");
        post.setHeader("Referer", "https://twitter.com/");
        post.setHeader("Content-Type", "application/x-www-form-urlencoded");
        post.setEntity(new UrlEncodedFormEntity(paramList));
        // Execute POST data
        HttpResponse res = client.execute(post);

        int responseCode = res.getStatusLine().getStatusCode();

        System.out.println("\nSending 'POST' request to URL : " + url);
        System.out.println("Post parameters : " + paramList);
        System.out.println("Response Code : " + responseCode);
        System.out.println("Headers: "+res.getAllHeaders().toString());
        System.out.println("Response: "+res.getStatusLine());

        BufferedReader rd1 = new BufferedReader(
                    new InputStreamReader(res.getEntity().getContent()));

        StringBuffer resul = new StringBuffer();
        String line1 = "";
        while ((line1 = rd1.readLine()) != null) {
            resul.append(line1);
        }
        Document doc2 = Jsoup.parse(res.toString());
        System.out.println(doc2);
    }
            public static void main(String[] args) throws ClientProtocolException, IOException{
           Browser b = new Browser();

           b.login("https://twitter.com/login");
}

我相信需要 POST 的所有内容都已存在,例如用户名、密码以及真实性 token 。

最佳答案

原来我在 POST 请求中发送了错误的 session 信息!如果其他人有类似的问题,我建议使用 Chrome 开发者工具来检查发送/接收的 header 。

enter image description here

关于java - HTTP/1.1 400 错误请求 Apache,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21764533/

相关文章:

google-chrome - Chrome 忽略 ETag header ,只使用内存缓存/磁盘缓存

Node.js服务器: HTTP POST body is empty

java - Java Application Server有哪些Web服务器无法做到的具体用途?

apache - seo 友好 url 的 htaccess 问题 - GoDaddy

java - 执行executeQuery时结果集始终为空

php - - 大量 MySQL 丢失连接和连接失败

apache - 是否有管理 https ://behavior? 的 Apache/Plesk 服务器设置

python - 是否有一个 python http-client 可以自动执行多种身份验证类型的身份验证?

java - 在单个请求中获取多个用户的最新推文

java - 当前填充混合类型字节数组的最佳方法