Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > J2EE
|
J2EE General J2EE (Java 2 Enterprise Edition) discussions. Questions not specific to EE will be redirected elsewhere.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the J2EE section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 22nd, 2006, 11:00 PM
Registered User
 
Join Date: Jan 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problem setting request properties

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:

===
Code:
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 ?





Similar Threads
Thread Thread Starter Forum Replies Last Post
setting Collection properties for user control PurpleHaze VS.NET 2002/2003 2 December 30th, 2008 02:41 AM
problem with FileUpload and request Parameters sira Servlets 1 July 22nd, 2006 09:32 PM
Problem with request.form. shankar_v1983 ASP.NET 2.0 Basics 1 January 28th, 2006 10:15 AM
Problem with Request.QueryString mg1966 Classic ASP Databases 4 January 7th, 2005 02:04 PM
Setting the border properties in datagrid. katsarosj ASP.NET 1.0 and 1.1 Basics 2 November 11th, 2003 01:16 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.