如何使用java发送post表单?

如何使用java发送post表单?,java,html,post,Java,Html,Post,我想在一个网站上用java发送一个post表单。我想到了这个,但我不知道下一步该怎么做,或者这是否是正确的方法 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); p

我想在一个网站上用java发送一个post表单。我想到了这个,但我不知道下一步该怎么做,或者这是否是正确的方法

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);
post表单如下所示

<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>

科比·布莱恩特
德怀恩韦德

< /代码> 您可能想考虑使用。它有
HttpPost
类,非常易于使用。

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

或者您可以尝试以下代码:

// 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 = newOutputStreamWriter(uc.getOutputStream(), "8859_1");   
  out.write("username=bob&password="+password+"");   
  // remember to clean up   
  out.flush();   
  out.close();

您可以编写类似以下内容的代码:

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

使用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();

com.konghq
unirest java
3.1.02
独立的
Unirest.config().verifySsl(false);
HttpResponse response=Unirest.post(“http://127.0.0.1")
.字段(“时间戳”,“15711971524”)
.字段(“appId”、“xxx”)
.字段(“符号”,“3e345613c687110ebf437da23ad5a13f”)
.asString();

我发现这里的解决方案很有帮助:使用PostMethod对于当前的commons(第4版)来说已经过时,使用UrlEncodedFormEntity:@JonnyJD,从链接的响应来看,DefaultHttpClient似乎不推荐使用…;)来自评论:嗨,请不要仅仅用源代码回答。试着提供一个关于你的解决方案如何工作的很好的描述。请参阅:。谢谢