Just need someone familiar with HTTPClient to check over a piece of code

Posted by jax on Stack Overflow See other posts from Stack Overflow or by jax
Published on 2010-06-06T08:23:24Z Indexed on 2010/06/06 9:12 UTC
Read the original article Hit count: 324

Filed under:

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

© Stack Overflow or respective owner

Related posts about java