0

我想从 java servlet 中访问 php 中的 URL。我只想向该 url 发送信息。我不需要去那个 url。我想留在我的页面中。无论如何要这样做。?

4

2 回答 2

1

您可以像在 http components doc中那样简单地在该 url 上发帖

PostMethod post = new PostMethod("http://jakarata.apache.org/");
NameValuePair[] data = {
      new NameValuePair("user", "joe"),
      new NameValuePair("password", "bloggs")
};
post.setRequestBody(data);
// execute method and handle any error responses.
...
InputStream in = post.getResponseBodyAsStream();
// handle response.
于 2013-06-07T11:33:42.377 回答
0

试试下面的代码

            URL url = new URL("your url");

            URLConnection connection = url.openConnection();
            connection.setConnectTimeout(5000); // time out
            connection.setDoOutput(true);
            PrintWriter out = new PrintWriter(new OutputStreamWriter(connection.getOutputStream()));
            String postData = "your post data";
            out.print(postData);
            out.close();
            String response  connection.getInputStream();

或者你可以使用

           Request.Post("your url").bodyForm(
                Form.form().add("parameter name", "value")
                .add("parameter name1", "value").build()).execute();
于 2013-06-07T11:48:51.333 回答