高手请帮忙,Http请求的post方法出现异常

sixi_fish 2005-12-30 12:58:56
在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();

请高手帮忙解答一下,谢谢
...全文
581 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
baffling 2006-01-09
  • 打赏
  • 举报
回复
gz
kingofhawks 2005-12-30
  • 打赏
  • 举报
回复
恭喜~
leeight 2005-12-30
  • 打赏
  • 举报
回复
谢谢了
cenlmmx 2005-12-30
  • 打赏
  • 举报
回复
学习了
wylsx 2005-12-30
  • 打赏
  • 举报
回复
Congratulations!
leekooqi 2005-12-30
  • 打赏
  • 举报
回复
up
msnsnd 2005-12-30
  • 打赏
  • 举报
回复
那就恭喜了啊。
sixi_fish 2005-12-30
  • 打赏
  • 举报
回复
搞定问题了,原来是缺少设置 httpCon.setRequestProperty("Content-Type","text/XML;");

怪不得会出现以上错误
msnsnd 2005-12-30
  • 打赏
  • 举报
回复
挺好玩的阿,还把名字后缀改成aspx,可以混淆视听阿,哈哈。
不过问题不懂,观望中。
sixi_fish 2005-12-30
  • 打赏
  • 举报
回复
刚才调试时又发现新的情况

String postData = A + B;
System.out.println("postData = " + postData);// 打印post的数据

当postData = "11111"时没有问题,返回200正常响应


postData = "<OrderDataSet><OrderInfo><OrderID>000536</OrderIDOrderInfo></OrderDataSet>" 时,就会出现以上错误,返回500
sixi_fish 2005-12-30
  • 打赏
  • 举报
回复
还有奇怪的一点是:

当我连接对方的测试环境时没有任何问题,但我连接对方的正式环境就出现以上异常,我问过对方,两个环境唯一的不同之处是,测试用IIS6,正式的是IIS5

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧