当前位置: 技术问答>java相关
请教:使用java.net中httpURLConnection类模拟post方法?、、全部分数送上!
来源: 互联网 发布时间:2015-02-04
本文导语: 以下代码有问题,请各位给出自己的正确代码,模拟像一个表单POST方法,如果网页 是一个jsp网页,可以检测post是否正确被发送。 URL destURL = new URL("http://192.168.68.234:8100/test.jsp"); String request = "name=n&password=p&submit=su...
以下代码有问题,请各位给出自己的正确代码,模拟像一个表单POST方法,如果网页
是一个jsp网页,可以检测post是否正确被发送。
URL destURL = new URL("http://192.168.68.234:8100/test.jsp");
String request = "name=n&password=p&submit=submit"; //post请求的串
HttpURLConnection urlConn = (HttpURLConnection)destURL.openConnection();
urlConn.setDoOutput(true);
urlConn.setDoInput(true);
urlConn.setUseCaches(false);
urlConn.setRequestProperty("Content-type",
"text/html");
urlConn.setRequestProperty("Content-length",Integer.toString(request.length()));
DataOutputStream outStream = new DataOutputStream(
urlConn.getOutputStream());
URLCon.setRequestMethod("POST");
outStream.writeBytes(request);
outStream.close();
是一个jsp网页,可以检测post是否正确被发送。
URL destURL = new URL("http://192.168.68.234:8100/test.jsp");
String request = "name=n&password=p&submit=submit"; //post请求的串
HttpURLConnection urlConn = (HttpURLConnection)destURL.openConnection();
urlConn.setDoOutput(true);
urlConn.setDoInput(true);
urlConn.setUseCaches(false);
urlConn.setRequestProperty("Content-type",
"text/html");
urlConn.setRequestProperty("Content-length",Integer.toString(request.length()));
DataOutputStream outStream = new DataOutputStream(
urlConn.getOutputStream());
URLCon.setRequestMethod("POST");
outStream.writeBytes(request);
outStream.close();
|
给你发到信箱里了。