在JAVA中点击URL而不重定向

时间:2013-06-07 11:21:05

标签: java php redirect

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

2 个答案:

答案 0 :(得分:1)

您可以像在http组件doc

中一样在该网址上发帖
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.

答案 1 :(得分: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();