如何使用HttpURLConnection传递变量

时间:2011-04-10 19:16:25

标签: java http jsp httpwebrequest

我有以下代码来获取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 = null;
      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;
      }
  }
}

但此代码仅复制指定URL的内容。

相反,如果我想连接到jsp页面,我可以动态获取该JSP页面发送的值...

例如,如果我有一个页面http://192.16.110.51/WelcomeProject/index.jsp

我应该能够传递一个变量,反过来index.jsp页面会传回我附加了hello world的变量或对变量进行一些操作并将结果发回给我。我怎么能实现这个?...如果不是HttpURLConnection ..我还有其他方法可以实现这一点。

2 个答案:

答案 0 :(得分:1)

这是一个修改过的例子:http://www.java2s.com/Code/JavaAPI/java.net/URLConnectionsetDoOutputbooleandooutput.htm(我删除了响应部分,你可能真的想要它回来)

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();
  }

}

答案 1 :(得分:0)

当您使用http客户端时,您正在自动化浏览器。浏览器对jsp页面一无所知。它不了解java。它所知道的只是HTML和HTTP。

如果JSP页面响应表单帖子,那么您可以发送带有表单参数的HTTP POST。

使用Apache Commons Httpclient比使用原始JRE要容易得多,所以我建议阅读。