高手请帮忙,Http请求的post方法出现异常
在HTTP请求中,把数据用Post方式给方,出现了如下错误:
java.io.FileNotFoundException: Response: '500: Internal Server Error' for url: 'http://**********************/*********.aspx'(地址用*号代替)
我的写法如下:
public int connectChinaPost(String A, String B) throws
Exception {
java.net.URL url = null;
java.net.HttpURLConnection httpCon = null;
String result = "";
try {
url = new java.net.URL(this.ChinaPostURL);//请求地址
System.out.println("ChinaPostURL = " + ChinaPostURL);
httpCon = (java.net.HttpURLConnection) url.openConnection();//http请求
httpCon.setRequestMethod("POST");
httpCon.setDoOutput(true);
//发送流文件
//将订单详细信息组成字符串
String postData = A + B;
System.out.println("postData = " + postData);// 打印地址
//post数据
PrintWriter printWriter = new PrintWriter(httpCon.getOutputStream(), true);
printWriter.write(postData);
printWriter.flush();
printWriter.close();
int responseCode = httpCon.getResponseCode();
System.out.println("responseCode = " + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) { //非200正常响应
BufferedReader in = new BufferedReader(new InputStreamReader(httpCon.
getInputStream(), "8859_1"));
StringBuffer sbuff = new StringBuffer(2048);
while (true) {
String strRtn = in.readLine();
if (strRtn == null) {
break;
}
else {
sbuff.append(strRtn + "\n");
}
}
result = sbuff.toString();
//分解返回结果
int order_result = this.parseResponseData(result);
in.close();
httpCon.disconnect();
return order_result;
}
else { //抛出异常
throw new Exception("cant connect china post URL");
}
}
catch (Exception ex) {
ex.printStackTrace();
throw ex;
}
finally {
if (httpCon != null) {
httpCon.disconnect();
httpCon = null;
}
}
}
如果我把以下这几句去掉,就会返回 200 响应
//post数据
PrintWriter printWriter = new PrintWriter(httpCon.getOutputStream(), true);
printWriter.write(postData);
printWriter.flush();
printWriter.close();
请高手帮忙解答一下,谢谢