59347

HTTPRequest Get Data in Java

Question:

<br /> I would like to do an HTTPRequest in Java and then get the data from the server (it's not a webpage the data come from a database).<br /> I try this but the getData doesn't work.<br /> Do you know how I can get the Data?<br />

public static void main(String args[]) throws Exception { URL url = new URL("http://ip-ad.com"); HttpURLConnection httpCon = (HttpURLConnection) url.openConnection(); System.out.println("Request method is " + httpCon.getData()); }

Thanks<br />

Answer1:

You can get the response body of the web request as an <a href="http://download.oracle.com/javase/6/docs/api/java/io/InputStream.html" rel="nofollow">InputStream</a> with:

httpCon.getInputStream();

From there it depends on what the format of the response data is. If it's XML then pass it to a library to parse XML. If you want to read it into a String see: <a href="https://stackoverflow.com/questions/5867975/reading-websites-contents-into-string/5868033#5868033" rel="nofollow">Reading website's contents into string</a>. Here's an example of writing it to a local file:

InputStream in = httpCon.getInputStream(); OutputStream out = new FileOutputStream("file.dat"); out = new BufferedOutputStream(out); byte[] buf = new byte[8192]; int len = 0; while ((len = in.read(buf)) != -1) { out.write(buf, 0, len); } out.close();

Answer2:

You can use <a href="http://jersey.java.net/" rel="nofollow">http://jersey.java.net/</a> .

It's a simple lib for your needs.

Recommend

  • How do I find where an HttpURLConnection is trying to redirect me to?
  • Showing Error :Attempt to invoke interface method 'int java.util.List.size()' on a null ob
  • How do i do the following curl command in Java
  • Google OAuth2 for an web application hosted behind NAT (intranet server without public IP)
  • 2-table interaction: insert, get result, insert
  • Multiple versions of iTunesArtwork in one project?
  • How to resolve docker host names (/etc/hosts) in containers
  • Should I be afraid to use UDP to make a client/server broadcast talk?
  • HttpURLConnection.getOutputStream() takes 20 seconds. Why?
  • Bash if statement with multiple conditions
  • Copy to all folders batch file?
  • Sencha Touch 2.0 Controller refs attribute not working?
  • Play WS (2.2.1): post/put large request
  • Get data from AJAX - How to
  • Cannot resolve symbol 'MyApi'
  • Seeking advice on Jetty HttpClient Hang
  • Initializer list vs. initialization method
  • Why is an OPTIONS request sent to the server?
  • Spring security and special characters
  • How to add date and time under each post in guestbook in google app engine
  • javascript inside java/jsp code
  • Trying to switch camera back to front but getting exception
  • Can a Chrome extension content script make an jQuery AJAX request for an html file that is itself a
  • Circular dependency while pushing http interceptor
  • Is there a mandatory requirement to switch app.yaml?
  • bootstrap to use multiple ng-app
  • How to get icons for entities from eclipse?
  • File upload with ng-file-upload throwing error
  • ExecuteAsync RestSharp to allow backgroundWorker CancellationPending c#
  • Android Studio and gradle
  • AngularJs get employee from factory
  • Free memory of cv::Mat loaded using FileStorage API
  • How to set the response of a form post action to a iframe source?
  • Angular 2 constructor injection vs direct access
  • Change div Background jquery
  • Turn off referential integrity in Derby? is it possible?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • JaxB to read class hierarchy
  • Programmatically clearing map cache
  • How can i traverse a binary tree from right to left in java?