Igal
Igal

Reputation: 4783

How to get a HttpResponse text

I'm uploading a file by using rest, and getting a response from the server, in case and the upload succeed (response code 200) I'm also getting a guid for this operation, the header looks like this :

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: text/plain;charset=ISO-8859-1
Content-Length: 36
Date: Wed, 26 Jun 2013 07:00:56 GMT

**772fb809-61d5-4e12-b6f2-133f55ed9ac7** // the guid

I was wonder how can I pull out this guid ? should I use getInputStream() ?

10x

Upvotes: 1

Views: 2814

Answers (2)

fmodos
fmodos

Reputation: 4468

You can use one BufferedReader, here is one example:

InputStream inputStream = conn.getInputStream(); 
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream)); 
while(br.ready()){ 
    String line = br.readLine(); 
    //line has the contents returned by the inputStream 
}

Upvotes: 1

Juned Ahsan
Juned Ahsan

Reputation: 68715

From the response you have shared, it seems that you are getting the guid in the body and not in header. Headers are generally name-value pair.

You need to read the response body and get the guid. If you guid is the only thing in the response then you can do somehting like this:

URL url = new URL("http://yourwebserviceurl");
URLConnection con = url.openConnection();
InputStream in = con.getInputStream();
String encoding = con.getContentEncoding();
encoding = encoding == null ? "UTF-8" : encoding;
String guid = IOUtils.toString(in, encoding);
System.out.println(guid );

IOUtils is from apache.

Upvotes: 0

Related Questions