Solved

adding a header field in UrlConnection

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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.
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

759 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

21 Experts available now in Live!

Get 1:1 Help Now