如何用java发送post表单?

发布于 2024-12-07 02:54:28 字数 584 浏览 1 评论 0原文

我想在网站上用java发送帖子表格。我想出了这个,但我不知道下一步该做什么,也不知道这是否是正确的方法。

URL url = new URL("http://127.0.0.1");
URLConnection conn=url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);

帖子表格看起来像这样。

<form action="prikaz4.php" method="post">
    <select name="igralec"/>
    <option value="Kobe Bryant">Kobe Bryant</option>
    <option value="Dwayne Wade">Dwayne Wade</option>
    <input type="submit" />
</form>

I would like to send a post form with java on a website. I came up with this, but I dont what to do next or if this is even the right way.

URL url = new URL("http://127.0.0.1");
URLConnection conn=url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);

the post form looks like this.

<form action="prikaz4.php" method="post">
    <select name="igralec"/>
    <option value="Kobe Bryant">Kobe Bryant</option>
    <option value="Dwayne Wade">Dwayne Wade</option>
    <input type="submit" />
</form>

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

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

发布评论

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

评论(4

最近可好 2024-12-14 02:54:28

您可以编写与此类似的代码:

 import org.apache.commons.httpclient.HttpClient;
 import org.apache.commons.httpclient.HttpException;
 import org.apache.commons.httpclient.HttpStatus;
 import org.apache.commons.httpclient.methods.PostMethod;
 import org.apache.http.impl.client.HttpClients;

public class PostReqEx {

  public void sendReq(String url,String email,String fname){
    HttpClient httpClient = HttpClients.createDefault();
    PostMethod postMethod = new PostMethod(url);
    postMethod.addParameter("Email", email);
    postMethod.addParameter("fname", fname);
    try {
        httpClient.executeMethod(postMethod);
    } catch (HttpException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    if (postMethod.getStatusCode() == HttpStatus.SC_OK) {
        String resp = postMethod.getResponseBodyAsString();
    } else {
         //...postMethod.getStatusLine();
    }
  }
}

You can write code similar to this :

 import org.apache.commons.httpclient.HttpClient;
 import org.apache.commons.httpclient.HttpException;
 import org.apache.commons.httpclient.HttpStatus;
 import org.apache.commons.httpclient.methods.PostMethod;
 import org.apache.http.impl.client.HttpClients;

public class PostReqEx {

  public void sendReq(String url,String email,String fname){
    HttpClient httpClient = HttpClients.createDefault();
    PostMethod postMethod = new PostMethod(url);
    postMethod.addParameter("Email", email);
    postMethod.addParameter("fname", fname);
    try {
        httpClient.executeMethod(postMethod);
    } catch (HttpException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    if (postMethod.getStatusCode() == HttpStatus.SC_OK) {
        String resp = postMethod.getResponseBodyAsString();
    } else {
         //...postMethod.getStatusLine();
    }
  }
}
束缚m 2024-12-14 02:54:28

Apache 的 HttpClient 项目将为您更好地处理这个问题。

或者你可以尝试这个代码:

// Using java.net.URL and  
    //java.net.URLConnection  
    URL url = new URL("http://jobsearch.dice.com/jobsearch/jobsearch.cgi");   
    URLConnection connection = url.openConnection();
    connection.setDoOutput(true);  
    OutputStreamWriter out = new OutputStreamWriter(uc.getOutputStream(), "8859_1");   
    out.write("username=bob&password="+password+"");   
    // remember to clean up   
    out.flush();   
    out.close();

Apache's HttpClient project will handle this better for you.

or you can try this code:

// Using java.net.URL and  
    //java.net.URLConnection  
    URL url = new URL("http://jobsearch.dice.com/jobsearch/jobsearch.cgi");   
    URLConnection connection = url.openConnection();
    connection.setDoOutput(true);  
    OutputStreamWriter out = new OutputStreamWriter(uc.getOutputStream(), "8859_1");   
    out.write("username=bob&password="+password+"");   
    // remember to clean up   
    out.flush();   
    out.close();
幻想少年梦 2024-12-14 02:54:28

您可能需要考虑使用 Apache 的 HttpClient 库。它有 HttpPost 类,非常易于使用。

You may want to consider using HttpClient library from Apache. It's got HttpPost class, which is very easy to use.

苏别ゝ 2024-12-14 02:54:28

使用 unirest 很容易。

<dependency>
    <groupId>com.konghq</groupId>
    <artifactId>unirest-java</artifactId>
    <version>3.1.02</version>
    <classifier>standalone</classifier>
</dependency>



Unirest.config().verifySsl(false);
        HttpResponse response = Unirest.post("http://127.0.0.1")
                .field("timestamp", "1571971524")
                .field("appId", "xxx")
                .field("sign", "3e345613c687110ebf437da23ad5a13f")
                .asString();

It's easy to use unirest .

<dependency>
    <groupId>com.konghq</groupId>
    <artifactId>unirest-java</artifactId>
    <version>3.1.02</version>
    <classifier>standalone</classifier>
</dependency>



Unirest.config().verifySsl(false);
        HttpResponse response = Unirest.post("http://127.0.0.1")
                .field("timestamp", "1571971524")
                .field("appId", "xxx")
                .field("sign", "3e345613c687110ebf437da23ad5a13f")
                .asString();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文