Solved

adding a header field in UrlConnection

Posted on 2000-03-13
3
520 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 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
json example 39 135
what is a "java.lang.System Property"   ? 20 57
MySqlDump not dumping triggers 1 19
type mismatch (Object[] to double[] 4 13
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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 learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

863 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

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now