Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

adding a header field in UrlConnection

Posted on 2000-03-13
3
Medium Priority
?
527 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
  • 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 90 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
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:
Suggested Courses

876 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