HttpURLConnection POST 数据错误,为什么?在本地测试很好呀???模拟一个投票机器人,但老有问题,请高手指点。具体做法是:
通过java 模拟网页表单提交数据,方法为 POST。 表单数据:
<FORM name=form1 action="http://px.chinamushroom.net/ping_ok.asp" method="post"> <INPUT name="T2" value="[email protected]"> <input type="checkbox" name="pt00" value="101">
<input type="checkbox" name="pt00" value="102">
<input type="checkbox" name="pt00" value="103">
<input type="checkbox" name="pt00" value="104">
<input type="checkbox" name="pt00" value="105"> <INPUT class=itm type=submit value=" 我要投票 " name=tpok> </form>
// java 测试类 Run :package start;import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.Authenticator;
import java.net.URLEncoder;
import java.io.*;public class Run { /**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
URL url = new URL("http://px.chinamushroom.net/ping_ok.asp");
//URL url = new URL("http://192.168.0.101/post.asp");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)");
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setDoInput(true);
PrintWriter out = new PrintWriter(connection.getOutputStream());
// encode the message
String name = "pt00="+URLEncoder.encode("111, 108, 94, 102, 107, 100, 109", "UTF-8");
String submit="tpok="+URLEncoder.encode(" 我要投票 ","GBK");
String email = "T2="+URLEncoder.encode("[email protected]", "UTF-8");
// send the encoded message
out.println(name+"&"+email+"&"+submit);
out.close();
BufferedReader in= new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
in.close();
}}错误提示:
<script>alert('请选择投票项目最少五项。');</script><script>history.go(-1);</script>怎么办? 是通过 post 方法写入数据有问题呀???在网页中按要求操作可以正确提交。请大虾指点。。
通过java 模拟网页表单提交数据,方法为 POST。 表单数据:
<FORM name=form1 action="http://px.chinamushroom.net/ping_ok.asp" method="post"> <INPUT name="T2" value="[email protected]"> <input type="checkbox" name="pt00" value="101">
<input type="checkbox" name="pt00" value="102">
<input type="checkbox" name="pt00" value="103">
<input type="checkbox" name="pt00" value="104">
<input type="checkbox" name="pt00" value="105"> <INPUT class=itm type=submit value=" 我要投票 " name=tpok> </form>
// java 测试类 Run :package start;import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.Authenticator;
import java.net.URLEncoder;
import java.io.*;public class Run { /**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
URL url = new URL("http://px.chinamushroom.net/ping_ok.asp");
//URL url = new URL("http://192.168.0.101/post.asp");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)");
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setDoInput(true);
PrintWriter out = new PrintWriter(connection.getOutputStream());
// encode the message
String name = "pt00="+URLEncoder.encode("111, 108, 94, 102, 107, 100, 109", "UTF-8");
String submit="tpok="+URLEncoder.encode(" 我要投票 ","GBK");
String email = "T2="+URLEncoder.encode("[email protected]", "UTF-8");
// send the encoded message
out.println(name+"&"+email+"&"+submit);
out.close();
BufferedReader in= new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
in.close();
}}错误提示:
<script>alert('请选择投票项目最少五项。');</script><script>history.go(-1);</script>怎么办? 是通过 post 方法写入数据有问题呀???在网页中按要求操作可以正确提交。请大虾指点。。
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货