您的位置:首页 > 编程语言 > Java开发

java传输soap

2014-04-08 20:41 316 查看
public String postSoap(String soap, String requestName) throws IOException {
// Post请求的url,与get不同的是不需要带参数
URL postUrl = new URL(POST_URL);
// 打开连接
HttpURLConnection connection = (HttpURLConnection) postUrl
.openConnection();
logger.info(requestName + " request: " + soap);

connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
connection.setRequestProperty("SOAPAction", "http://ispp.com.cn/ispp_npi/SYNNPIAPI");

StringBuilder result = new StringBuilder();
BufferedReader reader = null;
try {
connection.connect();
DataOutputStream out = new DataOutputStream(connection
.getOutputStream());
out.write(soap.getBytes("UTF-8"));

out.flush();
out.close(); // flush and close
reader = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
} catch (Exception e) {
logger.error(requestName + " error: ", e);
}finally{
reader.close();
connection.disconnect();
}

return result.toString();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: