HttpUrlConnection's setRequestProperty() doesn't work
Hi,
I am trying to set the request properties of a HttpUrlConnection to specific settings before I download the given page. however, the response code is 200(OK) for pages that have different property from what I set. For Example, I set
connection.setRequestProperty("Accept","image/jpg");
connection.setRequestProperty("Accept-charset","utf-8");
for the connection and ask for the page with accept :text/html and accept-charset:iso-8859-1, but the response is 200(Ok).
can anybody tell what is wrong with my code, the code is as follows:
import java.net.URL;
import java.net.URLConnection;
import java.net.HttpURLConnection;
import java.util.Properties;
class Headok
{
private int code;
String proxy = "proxy.xxx.xxx.xx";
String port = "8080";
public boolean headOk(URL HeadUrl)
{
try
{
Properties systemproperties = System.getProperties();
systemproperties.setProperty("http.proxyHost",proxy);
systemproperties.setProperty("http.proxyPort",port);
//URLConnection HeadUrl = new URLConnection(url);
HttpURLConnection connection = (HttpURLConnection)HeadUrl.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("HEAD");
connection.setRequestProperty("Accept","image/jpg");
connection.setRequestProperty("Accept-charset","utf-8");
connection.setRequestProperty("From","tmindaye@xxc.xxx.xxx.xx");
connection.setRequestProperty("Connection","Keep-Alive");
connection.connect();
code=connection.getResponseCode();
//System.out.println(code);
if (code==connection.HTTP_OK)
{
return true;
}
}
catch (Exception e)
{
return false;
}
return false;
}
public static void main (String args[]) throws Exception
{
Headok head = new Headok();
boolean k=head.headOk(new URL("http://news.bbc.co.uk/"));
if(k==true)
{
System.out.println("ok");
}
else{
System.out.println("not-ok");
}
}
}
[2150 byte] By [
ethioa] at [2007-11-26 17:21:49]

# 3
Dear ethiio,
First a big thanks for your keen in knowing this problem. Please help me in this issue. I am breaking my head for the past four days. Here i explain it in detail... All the sites i have referred does the same and i am also following the same but not getting the solution.
SCENARIO:
I want to mimic a browser using the java code. i will supply the first page url then the java code should read the html of the given url. then the second url and it should do the same.... then the third url....fourth and so on...
Problem:
The code works fine... but the problem is : when i give the second url i am getting a cookie in the response header. I am able to read header and get the "Set-Cookie" value. I am using setRequestProperty("Cookie",cookie) to set the read value of cookie to next request. But instead of getting the html of the url i supplied i am gettin the html of "ERROR PAGE" which contains the message as follows: "Cookies Should be enabled for this service, Please enable cookies in your browser" i dont know where i am going wrong... Please help me to fix this problem
SOURCE CODE:
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;
public class UrlCheck
{
int stepCount=1;
int fileSuffix=1;
int cookieSuffix=1;
URL url = null;
BufferedReader bufferedReader = null;
boolean isCookieFound=false;
String copyCookie=null;
public static void main(String[] urls) throws MalformedURLException,IOException
{
System.getProperties().put("proxySet","true");
System.getProperties().put("proxyHost","a.b.c.d");// a,b,c,d is not the actual value
System.getProperties().put("proxyPort","X");//X is not the actual value
UrlCheck readHtml = new UrlCheck();
while(readHtml.stepCount<6)
{
readHtml.url=new URL(readHtml.urlDesigner());
readHtml.cookieAndContent();
}
}
public String urlDesigner()
{
String url=null;
switch(stepCount)
{
case 1:
url="URL 1";
stepCount++;
break;
case 2:
url="URL 2";
stepCount++;
break;
case 3:
url="URL 3";
stepCount++;
break;
case 4:
url="URL 4";
stepCount++;
break;
case 5:
url="URL 5";
stepCount++;
break;
}
return url;
}
public void cookieAndContent() throws IOException
{
int n=1;
String cookie=null;
String temp=null;
boolean done = false;
FileOutputStream fileOutputStream = new FileOutputStream("D:/Cookie/Cookie"+cookieSuffix+".txt/");
DataOutputStream dataOutputStream= new DataOutputStream(fileOutputStream);
FileOutputStream fileOutputStream1 = new FileOutputStream("D:/ReadHtml/Page"+fileSuffix+".txt/");
DataOutputStream dataOutputStream1= new DataOutputStream(fileOutputStream1);
HttpURLConnection connection = (HttpURLConnection) (url.openConnection());
if(isCookieFound)
{
System.out.println("-"+copyCookie+"-");
connection.setUseCaches(false);
connection.setDefaultUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Cookie",copyCookie);
PrintWriter pw = new PrintWriter(connection.getOutputStream());
pw.flush();
pw.close();
}
connection.connect();
InputStream ip = connection.getInputStream();
/**************Header Value Getter************************/
while (!done)
{
String headerKey = connection.getHeaderFieldKey(n);
String headerVal = connection.getHeaderField(n);
if (headerKey!=null || headerVal!=null)
{
//System.out.println(headerKey+"="+headerVal);
dataOutputStream.writeBytes(headerKey+"+"+headerVal);
}
else
{
done = true;
cookie=connection.getHeaderField("Set-Cookie");
//copyCookie=cookie;
if(cookie!=null)
{
int index = cookie.indexOf(";");
if(index >= 0)
{
isCookieFound=true;
cookie = cookie.substring(0, index);
copyCookie=cookie;
System.out.println("##########"+cookie+"##########");
dataOutputStream.writeBytes("****************"+cookie+"****************");
}
}
}
n++;
}
cookieSuffix++;
//System.out.print(copyCookie);
/**************Content Value Getter************************/
int ch;
char value;
while((ch=(ip.read()))!=-1)
{
value=(char)ch;
//System.out.print(value);
dataOutputStream1.writeBytes(String.valueOf(value));
}
fileSuffix++;
ip.close();
connection.disconnect();
System.out.println("*********"+isCookieFound+"*********");
}
}
Message was edited by:
Lokee