如何使用 HttpURLConnection 传递变量

发布于 2024-10-31 17:45:05 字数 2558 浏览 1 评论 0原文

我有以下代码来获取 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 页面会将我传回附加有的变量你好世界或对变量进行一些操作并将结果发回给我。我怎样才能实现这一目标?...如果不是 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 = 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;
      }
  }
}

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花桑 2024-11-07 17:45:05

这是修改自的示例: 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();
  }

}

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

}
违心° 2024-11-07 17:45:05

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

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

使用 Apache Commons Httpclient 比使用原始 JRE 更容易,因此我建议阅读相关内容。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文