如何使用java发送表单? [英] How to send post form with java?
本文介绍了如何使用java发送表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在网站上发送一个包含java的帖子。我想出了这个,但我不知道下一步该怎么做,或者如果这是正确的方式。
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>
推荐答案
您可以编写类似下面的代码:
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();
}
}
}
这篇关于如何使用java发送表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文