Make HTTP POST Or GET Request From Java

Filed under: — By Aviran Mordo @ 5:36 am

In this day of age where many applications need to communicate with web servers you need to make a simple HTTP call to the server. Usually There are two types of calls you’ll need to make an HTTP GET or HTTP POST.
There are many open source libraries to make http operations like Apache HttpClient. But if you don’t want to use a 3rd party library and want to write the code yourself you can do it without any problem.
So instead of blabbing words, here is a code sample of how you do it.

Code example (sorry for the lack of formatting):

package HTTPUtil;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLConnection;

public class HTTPRequestPoster
* Sends an HTTP GET request to a url
* @param endpoint - The URL of the server. (Example: " http://www.aviransplace.com")
* @param requestParameters - all the request parameters (Example: "param1=val1&param2=val2"). Note: This method will add the question mark (?) to the request - DO NOT add it yourself
* @return - The response from the end point
public static String sendGetRequest(String endpoint, String requestParameters)
String result = null;
if (endpoint.startsWith("http://"))
// Send a GET request to the servlet

// Send data
String urlStr = endpoint;
if (requestParameters != null && requestParameters.length () > 0)
urlStr += "?" + requestParameters;
URL url = new URL(urlStr);
URLConnection conn = url.openConnection ();

// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = rd.readLine()) != null)
result = sb.toString();
} catch (Exception e)
return result;

* Reads data from the data reader and posts it to a server via POST request.
* data - The data you want to send
* endpoint - The server's address
* output - writes the server's response to output
* @throws Exception
public static void postData(Reader data, URL endpoint, Writer output) throws Exception
HttpURLConnection urlc = null;
urlc = (HttpURLConnection) endpoint.openConnection();
} catch (ProtocolException e)
throw new Exception("Shouldn't happen: HttpURLConnection doesn't support POST??", e);
urlc.setRequestProperty("Content-type", "text/xml; charset=" + "UTF-8");

OutputStream out = urlc.getOutputStream();

Writer writer = new OutputStreamWriter(out, "UTF-8");
pipe(data, writer);
} catch (IOException e)
throw new Exception("IOException while posting data", e);
} finally
if (out != null)

InputStream in = urlc.getInputStream();
Reader reader = new InputStreamReader(in);
pipe(reader, output);
} catch (IOException e)
throw new Exception("IOException while reading response", e);
} finally
if (in != null)

} catch (IOException e)
throw new Exception("Connection error (is server running at " + endpoint + " ?): " + e);
} finally
if (urlc != null)

* Pipes everything from the reader to the writer via a buffer
private static void pipe(Reader reader, Writer writer) throws IOException
char[] buf = new char[1024];
int read = 0;
while ((read = reader.read(buf)) >= 0)
writer.write(buf, 0, read);



65 Responses to “Make HTTP POST Or GET Request From Java”

  1. Frank Says:

    Thanks, this has been useful.


  2. xavier Says:

    Hello!….I whant make 1 page of internet and i don’t know how …Can you help me?Plz contact me !!!

  3. Brian Says:

    How do I properly create the reader object for the postData function?

  4. Brian Says:

    Update: this works
    new StringReader(”type=1&keyword=abc”)

  5. elmih Says:

    thanks, this was really helpful!

  6. Dave Says:

    The “postData()” method does not work. I can’t find a sample on line that does. Every time I get the same thing:
    Server returned HTTP response code: 405 for URL

    If not that error, other samples give this:
    Unexpected end of file from server

    Why can’t people post working samples? IF I’m suppose to set headers to do a post expected by the server I hit, HOW do I find our what headers need to be set?

  7. Brian Says:

    URL formAction = new URL(”http://targetsite.com/search.php”);
    FileWriter fw = new FileWriter (”results.html”);
    postData(new StringReader(”type=1&keyword=abc”), formAction, fw);

  8. Anonymous Says:

    java method post example

  9. Xenome Says:

    Very strange … when i comment the part of the code concerned with printing the response the querystring is not sent and not received at the servlet i don’t understand why ????

  10. Lenin Says:

    From where do i actually read the response receive from the POST request?
    My code looks something like this:

    FileWriter fw = new FileWriter (”results.html”);
    URL formAction = new URL(”my url”);
    postData(new StringReader(”my arguments”),formAction,fw);

    There is no file such as results.html created in the current folder.

  11. bananahom Says:

    String file_name = “result.html”;

    URL formAction = new URL(””);
    FileWriter fw = new FileWriter(file_name);
    postData(new StringReader(xml_data), formAction, fw);

    i use the above code, it’s work!

  12. sufu Says:

    Hey, can you guys explain how to enter the xml_data? Is there any particular format or something for that? Also, wht is the file name that you mention in the FileWriter supposed to stand for?

  13. Aviran Mordo Says:

    you send xml_data as String just like any other data. The file_name is the file where the response from the server would be written to

  14. asd Says:

    you touch my trallalaaaaaaa

  15. Philip Says:

    In my application, I want to create a file and write some xml to it. Then aome other application procees it and delete it. So I may need to create a file and write. Using the above code where it is writing, since no file is there. I tried to run locally, it is not creating any file , so it could not read. some confusion, please give a reply

  16. johan Says:

    This is great.. Thanks.

  17. Abhishek Dilliwal Says:

    Nice one!!! thanks :)

  18. djslash Says:

    And if i have to save the content of a web site which has a sign-on screen? I have set the post method and the parameters but there will be just the sign-in screen in the html file. Please help me! :)

  19. Aviran Mordo Says:

    First you need to authenticate with the login page. After you authenticate use the HttpState and set it with the next http call



  20. leon Says:

    thanks works g8

  21. Nuno Says:

    Good afternoon,
    Thanks for this code. It was very useful. I need a server that can read the information sent in the variable “data”. That is, if I send a string with the variable data how can i do to read that information from the server side?
    thank you very much

  22. Aviran Mordo Says:

    In your servlet just read the InputStream coming from the HttpServletRequest

  23. billy Says:

    thanks for the code Aviran.I would like to send http requests to a server through gui components (e.g swing controls) then display results in lists.For example I want to enter search queries in a combo box in a client, then send the request to a server. if the request is successful my search results will be displayed in a list.

  24. srini Says:


    my requirement is when ever user submit the request it should invoke http post request which should append user name and pwd and form details should send as post body.

    can you please help how to do this.

  25. Aviran Mordo Says:

    Just look at Brian’s comment (#7)

  26. Patrick Says:

    Where can I find the HTTPUtil package? I can not seem to find it.

  27. Aviran Mordo Says:

    HTTPUtil is just a sample. Just copy the code and paste it into a file. You can rename the package name to be whatever you want.

  28. Akila Gunaratne Says:

    Thanks! This is very handy. Will be using it on the site.

  29. Anonymous Says:


  30. Anonymous Says:

    hi ty very much

  31. Sameer Says:

    When I run this its just displaying the webpage specified in the url, how can we get it to response data

  32. Aviran Mordo Says:

    In the postData method, just use the “output” to write the response data to any object you’d like. output is a writer, you can write it to anything

  33. Sameer Says:

    HI Aviran,

    I send a request to a login url with username and password by postDate method.I am getting the response as html code of login page but i expected “login success” .While using get method I am getting same result as html code of login page.

    Pls help me ….

  34. Aviran Mordo Says:

    If you are trying to login to a 3rd party service, you’ll need to understand what are the login requirements for that page. Maybe https maybe javascript enables, maybe cookies. I can not help you with that.

  35. gpl Says:

    Just to let you know your code has been a fantastic help

    Many many thanks

  36. David Says:

    Great example! Is there a way to retrieve the fully rendered HTML from a URL? For example, if the URL has a “document.write(”hello world”), I’m actually getting that exact text back. Ultimately, I want the final version that a browser would see - in this case, just “hello world”. TIA!

  37. Anonymous Says:

    Thanks, this is what I have been looking for for a long time!

  38. Anonymous Says:

    in my code i need to submit a fille to an url using post method.can u help me

  39. Aviran Mordo Says:

    If you need to send relatively small files you can base64 encode the file first and then send it in a POST request.
    However for files you’ll probably want to use multipart file upload

  40. Anonymous Says:

    Sorry I don’t understand how to use your postData method. Can you give me an example how to call this method from a main method?

  41. Aviran Mordo Says:

    Read comment #7 or #13

  42. Dan Says:

    Aviran, can you contact me I need some work done, I need to run HTTP POST GETs to a web application to retrieve data. I have documentation but I am not very technical. If you can do this work and want more information, email me. Thanks

  43. ram Says:

    Hi,it really Good……..

  44. blurb Says:

    Thank you very much this is the first example that actually worked

  45. EQLIT Says:

    post does not work! probably you need to set some http-headers, content-type to form-data???????

  46. EQLIT Says:


  47. achiume Says:

    Good stuff. Works like a charm with some adjustments to suit needs.
    Contact me if you are interested in contract work.

  48. antivirusfree Says:

    Thanks, this has been useful.

  49. Anthony Says:

    You declared StringBuffer twice, once as “data” and later as “sb.” “data” is unused.

  50. anshul Says:

    Thanks really nice article.

  51. Aviran Mordo Says:

    Thanks Anthony - fixed

  52. RJ Says:

    Code works, good post!

  53. Dhiraj Says:

    Very good example is this.. I have 1 Question.
    How can I Implement this for multiple URL and how to find that which URL uses which port?

  54. rajesh Says:


  55. rajesh Says:

    yasinsayalkhanmat \do apni jaan

  56. Kamis Says:

    Prosze o pomoc. 2344004 gadu gadu. Potrafie sie zalogowac na stronie przy pomocy metody POST, niestety nie radze sobie z wyslaniem GET;em parametrow ;/

  57. patrick Says:

    Thanks for the post! I am getting a 400 bad request when I run this. The url is works in a browser.
    This happens on the line:

    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));

    Has this happened to anyone? ideas?

  58. Aviran Mordo Says:

    I can’t really know, maybe the server requires cookies of some sort, or some kind of parameter or http header that you do not set properly

  59. satish Says:

    htanks it helped..

  60. Elly Says:

    Thank you! Very clear example which has been really useful to me :)

  61. reserve Says:

    Wow, awesome blog layout! How long have you been blogging for? you make blogging look easy. The overall look of your web site is excellent, as well as the content!. Thanks For Your article about Make HTTP POST Or GET Request From Java - Aviran’s Place .

  62. Keith Says:


    I am trying to use the code from this blog. I previously used ColdFusion and now need to translate that code to Java. In CF I used the following to POST to a site and then access the response:

    Going from the comments here I am using the following in Java:

    String file_name = “XMLSubscriber.xml”;

    URL formAction = new URL(etURL);
    FileWriter fw = new FileWriter(file_name);
    common.httprequest.postData(new StringReader(sXML), formAction, fw);

    I am not sure how to access the XMLSubscriber.xml file that should be written as the response in Java. without specifying a directory in the file_name String variable, is it written to the same directory my code is in? And I could simply access it by building a document where the file path is just the name of my XML file that is written by the response? Or should I adjust your code to return an object I can access in my code? So my code would have the line:

    myObject = common.httprequest.postData(new StringReader(sXML), formAction, fw);

    I am just confused on how to access the file that is written as the response from the secondary site. CF made it so easy, and Java does not make anything easy. Thanks for any help you can give.

  63. Keith Says:

    Apparently it did not like my CF code, let me try again omitting the ” to see if that helps:

    cfhttp url=”#etURL#” method=”POST” useragent=”#CGI.http_user_agent#” result=”objGet”
    cfhttpparam name=”xml” value=”#sXML.Trim()#” type=”formfield”/
    cfhttpparam name=”qf” value=”xml” type=”formfield”

    cfset XMLSubscriber = XmlParse(objGet.FileContent)

  64. Greg Says:

    I am attempting to create a servlet that post to a elaticsearch server using a query body from an xml file. This looks like it is what I need, however I think I am missing a few things here. How do I post to the specified server using xml data instead of appending the URL? Where do the results actually go? I know I am pretty new to java, but I don’t see any place where I could set the results to a variable, and use somewhere else in the code.

  65. Aviran Mordo Says:

    @Greg, The result will be in the “Writer output” object that you provide to the method.
    output is an object that implements the Writer interface, so the method will write the result to this object

Leave a Reply

You must have Javascript enabled in order to submit comments.

All fields are optional (except comment).
Some comments may be held for moderation (depends on spam filter) and not show up immediately.
Links will automatically get rel="nofollow" attribute to deter spammers.

Powered by WordPress