Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8700487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:10:45+00:00 2026-06-13T02:10:45+00:00

I am trying to make a HttpPost using the HttpClient(HttpComponents). This is my code:

  • 0

I am trying to make a HttpPost using the HttpClient(HttpComponents). This is my code:

try{
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost post = new HttpPost(
                "https://accounts.google.com/o/oauth2/token");
            post.addHeader("accept", "application/json");
            post.addHeader("Content-Type", "application/json");

            List<NameValuePair> nvps = new ArrayList<NameValuePair>();
            nvps.add(new BasicNameValuePair("code", new String(code.getBytes(), Charset.forName("UTF-8"))));
            nvps.add(new BasicNameValuePair("client_id", "googleClientId"));
            nvps.add(new BasicNameValuePair("redirect_uri", "http://localhost:8080/myapp/test"));
            nvps.add(new BasicNameValuePair("client_secret", "ClientSecret"));
            nvps.add(new BasicNameValuePair("grant_type", "authorization_code"));
            post.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));


            HttpResponse resp = httpClient.execute(post);

            if (resp.getStatusLine().getStatusCode() != 200) {
                throw new RuntimeException("Failed : HTTP error code : "
                   + resp.getStatusLine().getStatusCode());
            }

            BufferedReader br = new BufferedReader(
                             new InputStreamReader((resp.getEntity().getContent())));

            String output;
            System.out.println("Output from Server .... \n");
            while ((output = br.readLine()) != null) {
                System.out.println(output);
                res = res + output;
            }
            System.out.println(output);
            httpClient.getConnectionManager().shutdown();
            } catch(Exception e){
                e.printStackTrace();
            }
            System.out.println(res);

I get a 400 code. I suspect it is because all the parameters I set contain special characters, like _ . and / and they are getting escaped. How can I avoid this? Or am I missing the real problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T02:10:46+00:00Added an answer on June 13, 2026 at 2:10 am

    Shouldnt http header “Content-Type” be application/x-www-form-urlencoded since you are explicitly URL encoding the post parameters?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make Https connections on the Android phones, using HttpClient. Since the
i'm trying to make out with web-apps on java using google app engine. i
I'm trying to resume an upload using indy (HTTP Post), the code looks like
My code works for http, but not https. I'm trying to make Https connections
I am trying make a JSON call to a server using HttpClient API. The
I am trying to make a Http POST request using apache HTTP client. I
I'm trying to make http post request using the request module text = 'some
I'm trying make this code which is from a Dictionary of Arrays (CODE) from
I'm trying to make a simple HTTP post a endpoint with ONLY url arguments.
I am fairly new to iOS development and trying make a simple app which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.