【问题标题】:How to send post form with java?如何用java发送post表单?
【发布时间】:2011-11-26 17:58:33
【问题描述】:

我想在网站上发送一个带有 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>

【问题讨论】:

    标签: java html post


    【解决方案1】:

    您可能需要考虑使用HttpClient library from Apache。它有HttpPost 类,非常好用。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

        你可以编写类似这样的代码:

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

        【讨论】:

        【解决方案4】:

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

        【讨论】:

        • 点评来源: 您好,请不要只用源代码回答。尝试对您的解决方案如何工作提供一个很好的描述。请参阅:How do I write a good answer?。谢谢
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-08-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-24
        相关资源
        最近更新 更多