简体   繁体   中英

HTTP/1.1 400 Bad Request Apache

I'm attempting to login to twitter using the following code I've written. The issue is on each execution i receive a 400 Bad Request back as the response. I have tried numerous attempts to get this to work to no avail.

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");
}

I believe that everything that needs to be POST'd is being, such as the username, password, as well as the authenticity token.

Turns out i was sending the wrong session information in my POST request! If anyone else has a similar issue i recommend using Chrome Developer tools to inspect the headers being sent/received.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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