INet control behavior in Java

M

Marshall Cummings

Hello,

I have a Visual Basic application that communicates with a Java Servlet with
no problems. The code I am currently using to perform this simple
communication is as follows:

Dim strURL as String
Dim strFormData as String

strURL = "http://www.test.dnsalias.com/servlet/MyServlet"
strFormData = "message=This is a test."
netScore.Execute strURL, "Post", strFormData,"Content-Type:
application/x-www-form-urlencoded"

I need some Java code that does the same thing! Everything I try seems to
be wrong on some level or another - anyone know the correct way to do this?
I have tried things like the following:

URL url = new URL("http://java.sun.com/cgi-bin/backwards");
URLConnection connection = url.openConnection();
connection.setDoOutput(true);

PrintWriter out = new PrintWriter(connection.getOutputStream());
out.println("string=" + stringToReverse);
out.close();

When I look at the data sent over the wire using a packet sniffer, the
information sent with the VB app is very different than with the java app;
the java app seems to have encoded the information where the VB app is
sending it as plain text (and much more information, as well). Any ideas as
to what could be going wrong?

Any help would be greatly appreciated! Thanks!
 
D

Dag Sunde

Marshall Cummings said:
Hello,

I have a Visual Basic application that communicates with a Java Servlet with
no problems. The code I am currently using to perform this simple
communication is as follows:

Dim strURL as String
Dim strFormData as String

strURL = "http://www.test.dnsalias.com/servlet/MyServlet"
strFormData = "message=This is a test."
netScore.Execute strURL, "Post", strFormData,"Content-Type:
application/x-www-form-urlencoded"

I need some Java code that does the same thing! Everything I try seems to
be wrong on some level or another - anyone know the correct way to do this?
I have tried things like the following:

URL url = new URL("http://java.sun.com/cgi-bin/backwards");
URLConnection connection = url.openConnection();
connection.setDoOutput(true);

PrintWriter out = new PrintWriter(connection.getOutputStream());
out.println("string=" + stringToReverse);
out.close();

When I look at the data sent over the wire using a packet sniffer, the
information sent with the VB app is very different than with the java app;
the java app seems to have encoded the information where the VB app is
sending it as plain text (and much more information, as well). Any ideas as
to what could be going wrong?

The fololowing works for me...:

...

// URL of CGI-Bin script.
URL url = new URL ("http://java.sun.com/cgi-bin/backwards");

// URL connection channel.
HttpURLConnection connection =
(HttpURLConnection)url.openConnection();

// Let the RTS know that we want to do output.
connection.setDoOutput (true);

// No caching, we want the real thing.
connection.setUseCaches (false);

// Specify the content type.
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
connection.setRequestMethod("POST");

// Prepare the output stream....
PrintWriter out = new PrintWriter(connection.getOutputStream ());


String content = new String(URLEncoder.encode("string=" +
stringToReverse));
out.println(content);
out.flush ();
out.close ();

BufferedReader in = new BufferedReader(
new InputStreamReader(connection.getInputStream()));

String inputLine;
while((inputLine = in.readLine()) != null)
{
//DEBUG:
System.out.println(inputLine);
}
in.close();

...
 
D

Dag Sunde

Sorry... I forgot...

To Read the response as my example does, you need to add

// Let the run-time system (RTS) know that we want input.
connection.setDoInput (true);

too...
 
D

Dag Sunde

Myles said:
Hello Dag - thanks for the info, but I get exactly the same results. When I
look at the results in the data sent using a sniffer, it looks like garbage,
while the data sent using the VB application (with the Microsoft Internet
Transfer Control) is readable and is recognized by my servlet. Is there
anything else you can think of to try? Anyone else?

Thanks again for the help!

What does this part of the code print?
The String Reversed?

Put some debug-statements into .../cgi-bin/backwards
and see what it receives.

Remember that Java *always* operates with Unicode strings
 
M

Myles

Hello Dag - thanks for the info, but I get exactly the same results. When I
look at the results in the data sent using a sniffer, it looks like garbage,
while the data sent using the VB application (with the Microsoft Internet
Transfer Control) is readable and is recognized by my servlet. Is there
anything else you can think of to try? Anyone else?

Thanks again for the help!


MBC


Dag Sunde said:
Sorry... I forgot...

To Read the response as my example does, you need to add

// Let the run-time system (RTS) know that we want input.
connection.setDoInput (true);

too...
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top