4

I'm seeking a better way to extract data from a String that contains a HTTP header. For example, I'd like to get the number 160 from the content length portion of the string: "Content-Length: 160\r\n".

It appears that all the data in the HTTP header is preceded with a name, colon and space, and after the value immediately follows the '\r' and '\n' characters.

At the moment I am doing this:

int contentLengthIndex = serverHeader.lastIndexOf("Content-Length: ");
int contentLastIndex = serverHeader.length()-1;
String contentText = serverHeader.substring(contentLengthIndex + 16, contentLastIndex);
contentText = contentText.replaceAll("(\\r|\\n)", "");
int contentLength = Integer.parseInt(contentText);

But it seems messy and it is only good for getting the "Content-Length" at the end of the string. Is there a better more universal solution for extracting values from a String containing a HTTP header that can be adjusted to work for obtaining both int values or String values?

I should also mention that the connection needs to be able return data back to the browser after a request, which from my understanding prevents me from reaping the benefits of using HttpURLConnection.

4
  • how you are getting the headers? Commented Feb 10, 2013 at 16:54
  • @SuKu, I'm storing them character by character into a String. I'm away from the code right now but I believe it's through InputStreamReader. Commented Feb 10, 2013 at 17:14
  • definitely it has to be some Reader but what is the source of that Reader,I mean you can proceed using HttpURLConnection the way i have mentioned in my answer. Commented Feb 10, 2013 at 17:17
  • @SuKu, Unfortunately I can't use HttpURLConnection since I'm going to need to use to the connection to send data back to the requesting browser. Commented Feb 10, 2013 at 18:44

4 Answers 4

2

A quick solution will be:

new Scanner(serverHeader).useDelimiter("[^\\d]+").nextInt());

The other way if you want to create a Hashtable of the headers:

 String[] fields = serverHeader.trim().split(":");

 String key = fields[0].trim();
 String value = fields[1].trim();

I am not sure why you are doing this manual, there is already API for this!

Sign up to request clarification or add additional context in comments.

5 Comments

There seems to be an extra paren.
This appears as though it will only works for int values of name/value pairs, and it looks as though it'll only work if the "Content-Length" is known to be 160. I'd like to be able to extract by using the name from the name/value if there is a way.
I used the hard-coded string only as example, just put your variable there.
@iTech, which API are you referring to? I'm new to this so maybe I'm going about it the wrong way.
For example if you are using servlet you can just call request.getHeader(headerName). The code in your question does not show how you make your connection or if it client or server side
1

use Class java.net.HttpURLConnection

edited: also methods URLConnection.getContentLength() and URLConnection.getContentLengthLong()

2 Comments

I have to be able to use the connection to return data back to the browser after processing the GET request and I don't believe a HttpURLConnection allows for that.
There are methods URLConnection.getContentLength() and URLConnection.getContentLengthLong() URL url = new URL("yahoo.com/"); URLConnection connection = url.openConnection(); long connectionLength = connection.getContentLengthLong();
0

Have you tried just stripping all non-numeric characters from the string?

serverHeader = serverHeader.replaceAll("[^\\d.]", "");

1 Comment

I need to be able to get the value from a name/value pair for String values as well.
0

If you are using Socket class to read HTTP data i suggest you to use HttpURLConnection as it provides you convenient method to parse the Header Fields.

It has public Map getHeaderFields() method which you can use to get all the fields.

If you want a guide to start using HttpURLConnection you can see it here.

2 Comments

I started experimenting with that but ran into problems with sending data. I'm essentially writing a basic web server. Does HttpURLConnection allow you to take a HTTP request and use that connection to send a response that includes data such as an HTML or text file?
No you can't do that. But take a look here java-samples.com/showtutorial.php?tutorialid=236 . You can use similar method to parse your header.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.