开发者

How to pass variable with HttpURLConnection

I have the following code to get the contents of a URL..

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javaapplication3;

/**
 *
 * @author Ravi
 */
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;

public class Main {

  /**
   * @param args
   */
  public static void main(String[] args) {
      HttpURLConnection connection = nu开发者_Python百科ll;
      OutputStreamWriter wr = null;
      BufferedReader rd  = null;
      StringBuilder sb = null;
      String line = null;

      URL serverAddress = null;

      try {
          serverAddress = new URL("http://192.16.110.11:8084/RaviTest/index.jsp?id=3");
          //set up out communications stuff
          connection = null;

          //Set up the initial connection
          connection = (HttpURLConnection)serverAddress.openConnection();
          connection.setRequestMethod("GET");
          connection.setDoOutput(true);
          connection.setReadTimeout(10000);

          connection.connect();

          //get the output stream writer and write the output to the server
          //not needed in this example
          //wr = new OutputStreamWriter(connection.getOutputStream());
          //wr.write("");
          //wr.flush();

          //read the result from the server
          rd  = new BufferedReader(new InputStreamReader(connection.getInputStream()));
          sb = new StringBuilder();

          while ((line = rd.readLine()) != null)
          {
              sb.append(line + '\n');
          }

          System.out.println(sb.toString());

      } catch (MalformedURLException e) {
          e.printStackTrace();
      } catch (ProtocolException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      }
      finally
      {
          //close the connection, set all objects to null
          connection.disconnect();
          rd = null;
          sb = null;
          wr = null;
          connection = null;
      }
  }
}

But this code only copies the the contents of the specified URL.

Instead if I want to connect to a jsp page and can I get the values that are being sent by that JSP page dynamically...

For example if I have a page http://192.16.110.51/WelcomeProject/index.jsp

I should be able to pass a variable and in turn the index.jsp page will pass me back variable appended with hello world or do some manipulation on variable and send me back the result.How can I achieve this?...If not HttpURLConnection..Is there any other way through which I can accomplish this.


This is an example modified from: http://www.java2s.com/Code/JavaAPI/java.net/URLConnectionsetDoOutputbooleandooutput.htm (I remove the response part, you may actually want it back)

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.URL;
import java.net.URLConnection;

public class MainClass {
  public static void main(String args[]) throws Exception {
    String query = "id=3";

    URLConnection uc = new URL("http://192.16.110.11:8084/RaviTest/index.jsp").openConnection();
    uc.setDoOutput(true);
    uc.setAllowUserInteraction(false);
    DataOutputStream dos = new DataOutputStream(uc.getOutputStream());

    // The POST line, the Accept line, and
    // the content-type headers are sent by the URLConnection.
    // We just need to send the data
    dos.writeBytes(query);
    dos.close();
  }

}


When you are using an http client, you are automating, as it were, the browser. The browser doesn't know anything about jsp pages. It doesn't know about java. All it knows about is HTML and HTTP.

If the JSP page responds to form posts, then you can sent an HTTP POST with form parameters.

This is much easier with Apache Commons Httpclient than with the raw JRE, so I recommend reading up on that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜