HttpUrlConnection Problems

T

tony vee

Hi

I think my understanding of the HttpURLConnection must be wrong, so could
someone please put me straight.

I have a url:

String address = http://localhost:8080/reminder/servlet/Welcome

Which I want to connect to using an HttpURLConnection object:

URL outDialler = new URL(address);
HttpURLConnection outDiallerConnection =
(HttpURLConnection)outDialler.openConnection();
outDiallerConnection.setRequestProperty("name", "tony");
outDiallerConnection.connect();

but when the Welcome servlet at the other end of this connection receives
it, the value of the "name" parameter is always null! It is not just that
the name parameter is null, no name parameter has been sent at all!

I have tried using addRequestProperty("name", "tony") instead, and setting
the requestMethod to GET/POST, but all have been to no avail.

When I add the name/value arguments to the URL, the servlet gets the value
from the "name" parameter fine:

http://localhost:8080/reminder/servlet/Welcome?name=tony

Could someone please explain how to use the API or the
setRequestProperty(String name, String value) to set the URL parameters

Thanks

Tony

Ht
 
S

Silvio Bierman

tony vee said:
Hi

I think my understanding of the HttpURLConnection must be wrong, so could
someone please put me straight.

I have a url:

String address = http://localhost:8080/reminder/servlet/Welcome

Which I want to connect to using an HttpURLConnection object:

URL outDialler = new URL(address);
HttpURLConnection outDiallerConnection =
(HttpURLConnection)outDialler.openConnection();
outDiallerConnection.setRequestProperty("name", "tony");
outDiallerConnection.connect();

but when the Welcome servlet at the other end of this connection receives
it, the value of the "name" parameter is always null! It is not just that
the name parameter is null, no name parameter has been sent at all!

I have tried using addRequestProperty("name", "tony") instead, and setting
the requestMethod to GET/POST, but all have been to no avail.

When I add the name/value arguments to the URL, the servlet gets the value
from the "name" parameter fine:

http://localhost:8080/reminder/servlet/Welcome?name=tony

Could someone please explain how to use the API or the
setRequestProperty(String name, String value) to set the URL parameters

Thanks

Tony

Ht

setRequestProperty sets an HTTP header. That is not the same as a parameter.
If you are doing a GET add the parameter to the request URL. Otherwise you
should write the parameters form-encoded in the POST body.

Silvio Bierman
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top