Solved

adding a header field in UrlConnection

Posted on 2000-03-13
3
524 Views
Last Modified: 2006-11-17
Hi,

How do i add an extra field in the header while doing a POST from my java pgm. I am using a UrlConnection to POST the data.

Do I need to use UrlConnection.setRequestProperty() method ???


Thanx
ajay
0
Comment
Question by:ajayksh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 

Expert Comment

by:kbeier
ID: 2612329
Hi Ajay,

I'm not sure what you mean by "extra" field, but you can certainly set headers using UrlConnection.setRequestProperty().  For example:

//set the agent in the header
UrlConnection.setRequestProperty("User-Agent", "MyAgent/v1.00");

//set the "from" IP in the header        UrlConnection.setRequestProperty("From", IP);
                          UrlConnection.setRequestProperty("Accept", "text/html, text/plain");
                          UrlConnection.setRequestProperty("Accept-Charset", "iso-8859-1");
                          UrlConnection.setRequestProperty("Accept-Language", "en-gb");

You should read RFC1945 for more information about headers.
0
 
LVL 3

Expert Comment

by:yuvalg
ID: 2612346
What do you mean by "header" ?
Is that data that is transfered with each "message", or once per communication ?

yuval.
0
 

Accepted Solution

by:
kbeier earned 30 total points
ID: 2612566
HTTP Headers are messages sent between any agent (commonly your browser) and Internet web servers whenever you request a webpage. These headers contain information in fields.

As I understand it, every time you POST or GET something from a webserver, you should pass header information (fields) relating to your agent, what charset it accepts, what language it utilises etc to the web server which is serving you the page, but some of this information is not compulsory (perhaps someone could correct me here if I'm wrong - or further enlighten us all as to what is actually NECESSARY in a header since the RFC is rather vague about this! :-)).  For example, it's just good manners to let the web server you are POSTing to log your agent by passing it this info in the Agent field.  Otherwise you could just as easily pass your agent off as Mozilla or IE, which is what certain spam harvesters do...but that's off the topic. :-)
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question