如何用java发送帖子表格?

时间:2022-05-19 22:10:47

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.

我想在网站上发一篇带有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);

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>

3 个解决方案

#1


24  

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

#2


1  

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

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

#3


1  

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

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

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

#1


24  

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

#2


1  

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

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

#3


1  

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

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

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