我用下面的代码将数据提交到另一个网站的表单中,但是对方网站没有收到我提交的数据,我这显示也执行成功了,请大家帮我看看问题在哪?
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";
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();
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祝楼主成功。。
下载:commons-httpclient.jar
把代码改成:
String param = "seqId=1&servClass=1";
String url="http://192.168.0.106/ttk/buyTxPro.jsp?"+param;
HttpClient httpclient=new HttpClient();
PostMethod postMethod = new PostMethod(url);
NameValuePair[] postData = new NameValuePair[2];
postData[0] = new NameValuePair("seqId", "20081119122116421");
postData[1] = new NameValuePair("servClass", "1");
postMethod.addParameters(postData);
System.out.println("Ok");
postMethod.setRequestBody(url);
// 执行postMethod
int statusCode = 0;
try {
statusCode = httpclient.executeMethod(postMethod);
} catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("发送完成");