Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.Socket;
- import java.util.HashMap;
- import java.util.Map;
- public class HTTPResponse {
- public String responseLine;
- public int status;
- public String message;
- public long time;
- public int bodySize;
- public Map<String, String> header;
- public HTTPResponse(Socket connection) throws IOException, InvalidResponseException {
- BufferedReader bis = new BufferedReader(
- new InputStreamReader(connection.getInputStream()));
- header = new HashMap<String, String>();
- responseLine = bis.readLine();
- time = System.currentTimeMillis();
- if(responseLine == null)
- throw new InvalidResponseException();
- System.err.println(responseLine);
- String[] a = responseLine.split("\\s");
- if (a.length < 3 || !a[0].equals("HTTP/1.0"))
- throw new InvalidResponseException();
- status = Integer.parseInt(a[1]);
- message = a[2];
- String r;
- while(!(r = bis.readLine()).equals("")) {
- a = r.split(":", 2);
- if(a.length < 2)
- continue;
- header.put(a[0].toLowerCase(), a[1].trim());
- }
- String sz;
- if((sz = header.get("content-length")) != null) {
- bodySize = Integer.parseInt(sz);
- byte[] buff = new byte[bodySize];
- int k = 0, read = 0;
- while((k = connection.getInputStream().read(buff, read, bodySize - read)) != -1 && read < bodySize)
- read += k;
- assert read == bodySize : "Not all of file read. " + read + " " + bodySize;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment