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

  • Home
  • SEARCH
  • 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 8581135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:58:34+00:00 2026-06-11T20:58:34+00:00

I want to create an http request based on an URL in Java and

  • 0

I want to create an http request based on an URL in Java and change some of its headers or add new headers; then, receive the corresponding http response for that request and gets its header values and its content. How can I do this as simple as possible?

  • 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-11T20:58:35+00:00Added an answer on June 11, 2026 at 8:58 pm

    Indeed use apache httpclient 4.x and use a ResponseHandler.

    HttpClient has a lot of good stuff that you probably want that the raw Java API does not provide like multi threaded use, connection pooling, support for various authentication mechanisms, etc.

    Below is a simple example that executes a get and returns you the body as a String.

    import org.apache.http.HttpResponse;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.ResponseHandler;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.util.EntityUtils;
    
    ...
    
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet("http://www.google.com");
    httpGet.addHeader("MyHeader", "MyValue");
    
    try {
        String body = httpClient.execute(httpGet, new ResponseHandler<String>() {
    
            @Override
            public String handleResponse(HttpResponse response) throws IOException {
                Header firstHeader = response.getFirstHeader("MyHeader");
                String headerValue = firstHeader.getValue();
                return EntityUtils.toString(response.getEntity());
            }
        });
    } catch (IOException e) {
        e.printStackTrace();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a HTTP request from a specific port using C on
I want to create URL re-write rules for following URL in htaccess. http://example.com/vedios/category/fun/ ->
So I have Html like this http://trac.edgewall.org/wiki/RecentChanges (I want to create some Flash Track
I want create API which has same URL say http://stackoverflow.com/profile but having different request
I want to create an application like this: http://collabedit.com/ What is the most efficient
I want to create a pregmatch pattern which applies to: http://site.local/app/**/admin text. I created
I want to create an HTTPURLConnection to a PHP script and get the HTTP
I want to create WatermarkTextBox in WP7.1 I followed the link http://www.windowsphonegeek.com/articles/WP7-WatermarkedTextBox-custom-control But that
Here is the link : http://www.draw2d.org/draw2d/ I want to create flowchart as in the
I think everyone knows this site http://pinterest.com/ and I don't want to create site

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.