INet control behavior in Java

Discussion in 'Java' started by Marshall Cummings, Jan 20, 2004.

  1. 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!
    Marshall Cummings, Jan 20, 2004
    #1
    1. Advertising

  2. Marshall Cummings

    Dag Sunde Guest

    "Marshall Cummings" <> wrote in message
    news:1.gblx.net...
    > 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();

    ...

    --
    Dag.
    Dag Sunde, Jan 20, 2004
    #2
    1. Advertising

  3. Marshall Cummings

    Dag Sunde Guest

    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...


    --
    Dag.


    "Dag Sunde" <> wrote in message
    news:b3cPb.30943$...
    > "Marshall Cummings" <> wrote in message
    > news:1.gblx.net...
    > > Hello,
    > >
    > > I have a Visual Basic application that communicates with a Java Servlet

    > with

    <snipped />

    > > 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();
    >
    > ...
    >
    > --
    > Dag.
    >
    >
    Dag Sunde, Jan 20, 2004
    #3
  4. Marshall Cummings

    Dag Sunde Guest

    "Myles" <> wrote in message
    news:...
    > 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?
    > > BufferedReader in = new BufferedReader(
    > > new InputStreamReader(connection.getInputStream()));
    > >
    > > String inputLine;
    > > while((inputLine = in.readLine()) != null)
    > > {
    > > //DEBUG:
    > > System.out.println(inputLine);
    > > }
    > > in.close();

    >


    The String Reversed?

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

    Remember that Java *always* operates with Unicode strings

    --
    Dag.

    <snipped />
    Dag Sunde, Jan 21, 2004
    #4
  5. Marshall Cummings

    Myles Guest

    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" <> wrote in message
    news:X6cPb.30944$...
    > 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...
    >
    >
    > --
    > Dag.
    >
    >
    > "Dag Sunde" <> wrote in message
    > news:b3cPb.30943$...
    > > "Marshall Cummings" <> wrote in message
    > > news:1.gblx.net...
    > > > Hello,
    > > >
    > > > I have a Visual Basic application that communicates with a Java

    Servlet
    > > with

    > <snipped />
    >
    > > > 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();
    > >
    > > ...
    > >
    > > --
    > > Dag.
    > >
    > >

    >
    >
    Myles, Jan 21, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rad
    Replies:
    0
    Views:
    550
  2. Web Search Store

    inet vs. ie

    Web Search Store, Jan 21, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    446
    Web Search Store
    Jan 22, 2006
  3. Jan Jansen
    Replies:
    2
    Views:
    761
    Jan Jansen
    Feb 22, 2005
  4. Replies:
    0
    Views:
    521
  5. Hugh  Mandeville
    Replies:
    3
    Views:
    2,221
    shakah
    Jul 21, 2005
Loading...

Share This Page