Subject: Problem setting request properties
Posted By: Einherj Post Date: 1/22/2006 10:00:00 PM
Hi,

I'm using a Java Bean that opens a URLConnection to a remote url in order to request an HTML page. I want to set the Cookie and Referer headers in the request. Here is the code I use:

===

URL           url;
HttpURLConnection urlConnection = null;
InputStream   urlStream;
String        type;
String        content;
String          newContent;
String        upperCaseContent;

try {
  url = new URL(form_url);
} catch (MalformedURLException e) {
  return "MalformedURLException.";
}

if (url.getProtocol().compareTo("http") != 0)
  return "Protocol isn't http.";

try {
  // open the connection...
  urlConnection = (HttpURLConnection) url.openConnection();
  urlConnection.setAllowUserInteraction(false); 

  // set the cookies to send...
  urlConnection.setRequestProperty("Cookie", theCookies);

  // then, set the referrer
  urlConnection.setRequestProperty("Referer", pre_page_url);

  urlStream = url.openStream();

  type = urlConnection.guessContentTypeFromStream(urlStream);

  if (urlConnection == null)
    return "urlConnection is null";
  if (urlStream == null)
    return "urlStream doesn't contain ****.";

  // read in the entire URL content
  byte b[] = new byte[1000];
  int numRead = urlStream.read(b);
  content = new String(b, 0, numRead);
  while (numRead != -1) {
    numRead = urlStream.read(b);
    if (numRead != -1) {
      newContent = new String(b, 0, numRead);
      content += newContent;
    }
  }

  urlStream.close();


It's safe to assume that theCookies and pre_page_url Strings do contain what I want to send (I checked..). So what is wrong with my code ?

Go to topic 39045

Return to index page 388
Return to index page 387
Return to index page 386
Return to index page 385
Return to index page 384
Return to index page 383
Return to index page 382
Return to index page 381
Return to index page 380
Return to index page 379