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 850257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:14:09+00:00 2026-05-15T07:14:09+00:00

here are two little helper methods I have made for downloading files. I have

  • 0

here are two little helper methods I have made for downloading files. I have had to mix and match different tutorials of the web to get what I have here.

Now is there anything that I have done blatantly wrong here?

    public static InputStream simplePostRequest(URL url, List<NameValuePair> postData) throws ClientProtocolException, IOException {
    DefaultHttpClient httpclient = new DefaultHttpClient();  
    HttpPost postMethod=new HttpPost(url.toExternalForm());      
    postMethod.setEntity(new UrlEncodedFormEntity(postData, HTTP.UTF_8));
    HttpResponse response = httpclient.execute(postMethod);
    HttpEntity entity = response.getEntity();

    return entity.getContent();
}




public static InputStream simpleGetRequest(URL url, List<NameValuePair> queryString) throws ClientProtocolException, IOException {

    Uri.Builder uri = new Uri.Builder();
    uri.path(url.getPath());
    for(NameValuePair nvp: queryString) {
        uri.appendQueryParameter(nvp.getName(), nvp.getValue());
    }

    DefaultHttpClient httpClient = new DefaultHttpClient(); 
    HttpHost host = new HttpHost(url.getHost());
    HttpResponse response = httpClient.execute(host, new HttpGet(uri.build().toString()));
    HttpEntity entity = response.getEntity();

    return entity.getContent();
}
  • 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-05-15T07:14:09+00:00Added an answer on May 15, 2026 at 7:14 am

    I wouldn’t expect a huge response to a such a vague question. Why not write a pair of unit tests to try your code out instead?

    Anyway, the one thing that stands out for me based on my experience with HttpClient is that if subject to heavy load (large number of concurrent threads), your code seems unsafe – there seems to be no upper bound to the number of concurrent connections that could get created.

    If you think this might be relevant in your case, you could try something like this:

    class X {
    
      private static final HttpClient httpClient;
    
      static {
        SchemeRegistry defaultRegistery = new DefaultHttpClient().getConnectionManager()
                    .getSchemeRegistry();
        ThreadSafeClientConnManager connMgr = new ThreadSafeClientConnManager(defaultRegistery);
        connMgr.setMaxTotalConnections(10);
        connMgr.setDefaultMaxPerRoute(10);
        httpClient = new DefaultHttpClient(connMgr);
        httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 30000);
      }
    
      public static InputStream simpleGetRequest(URL url, List<NameValuePair> queryString) throws ClientProtocolException, IOException {...}
    
      public static InputStream simpleGetRequest(URL url, List<NameValuePair> queryString) throws ClientProtocolException, IOException {...}
    
    }
    

    … and just use the static httpClient in your methods instead of instantiating a new one every time.

    I copied and modified this from some code I’ve written before, but don’t consider this necessarily correct. The unit tests will be your friends here.

    EDIT: With regards to your comment about mixing URL and the HttpClient library class NameValuePair (is this your concern?), why not just a Map<String, String> in the method signatures?

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

Sidebar

Related Questions

I have a little issue here regarding the modification of Web.Config files in Feature
Hey, need a little help here. I have two models: class User < ActiveRecord::Base
Here is some of the main code for a little app I have made
Two questions here, second is a little more complicated than first. 1 - I
I have a question, here are two classes below: class Base{ public: virtual void
I have a very strange behavior with Request.Form . Here are two IIS 7
I have a menu bar that is rotated slightly. Here are two buttons as
enter code here I am having a little problem with jquery.empty(). I have some
I have two little problems with my UIScrollView. I managed to create scroll view,
Here is the little example of my two worksheets. Original Sheet : As you

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.