I have this code for reading webpage. I need to send http header to server because of identification. How should i do that? do i need to use socket? beacuse that’s what i found so far.
URL url = new URL("http://www.page.com/");
URLConnection yc = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
I found this code for sending http request header.
Socket sock = new Socket(url.getHost(), port);
DataOutputStream out = new DataOutputStream(new BufferedOutputStream(sock.getOutputStream()));
out.writeBytes("GET " + url.getFile() + " HTTP/1.0\n");
out.writeBytes("User-Agent: " + user_agent + "\n");
out.writeBytes("From: " + email_address + "\n");
out.writeBytes("Host: " + url.getHost() + "\n");
out.writeBytes("\n");
out.flush();
I dont know if i can combine this two codes somehow, or i need to change way of reading page to be able to send header.
Thanks!
It’s not clear what ‘because of identification’ means, but if you know what header fields you need, you can add them with: