java提交数据到其它网站
我用下面的代码将数据提交到另一个网站的表单中,但是对方网站没有收到我提交的数据,我这显示也执行成功了,请大家帮我看看问题在哪?
String sendstr = "http://192.168.0.106/ttk/buyTxPro.jsp";
String param = "seqId=1&servClass=1";
URL url = new URL(sendstr);
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "8859_1");
out.write(param);
out.flush();
out.close();
------解决方案--------------------
给个例子:
String sendstr = "http://192.168.0.106/ttk/buyTxPro.jsp";
sms = "SmsName=%CA%D6%BB%FA%B5%D8%CD% BC&Key=9bda4d11829361223e49106d717b4dcc&Mobile=13810175738&Message=%B2%E2%CA%D4%D0%C5%CF%A2"
try {
URL sendHttp = new URL(sendstr);
URLConnection uc = sendHttp.openConnection();
if (! (uc instanceof HttpURLConnection)) {
System.err.println("Wrong connection type");
return "Wrong connection type";
}
uc.setDoOutput(true);
uc.setUseCaches(false);
uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
String content = sms;
HttpURLConnection hc = (HttpURLConnection) uc;
hc.setRequestMethod("POST");
OutputStream os = uc.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
dos.writeBytes(content);
dos.flush();
dos.close();
InputStream is = uc.getInputStream();
int ch;
while ( (ch = is.read()) != -1) {
System.out.print( (char) ch);
}
is.close();
}
那么 ,你的参数param 就变成了这里的 sms,替换一下就够了。如果有中文,你可以用 URLEoncoder 来 encode()一下.
转自 : http://topic.csdn.net/t/20050823/14/4226252.html
祝楼主成功。。
------解决方案--------------------HttpClient吧