InputStreamReader isr = new InputStreamReader(is, "UTF-8");
----
贴上整个调用接口的代码,如下,也是一般的使用HttpURLConnection调用Webservice接口的代码
public String callInterface(String URL, String requestMethod, String contentType, String SOAPAction, String soapXml) throws IOException{
String result = "";
//第一步:创建服务地址
URL url = new URL(URL);
//第二步:打开一个通向服务地址的连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//第三步:设置参数
//3.1发送方式设置:POST必须大写
connection.setRequestMethod(requestMethod);
//3.2设置数据格式:content-type SOAPAction
connection.setRequestProperty("content-type", contentType);
connection.setRequestProperty("SOAPAction", SOAPAction);
//3.3设置输入输出,因为默认新创建的connection没有读写权限,
connection.setDoInput(true);
connection.setDoOutput(true);
//第四步:组织SOAP数据,发送请求
System.out.println("requestXml:"+soapXml);
connection.connect();//可能非必须
//将信息以流的方式发送出去
OutputStream os = connection.getOutputStream();
os.write(soapXml.getBytes("UTF-8"));
os.flush();
os.close();
//第五步:接收服务端响应,打印
int responseCode = connection.getResponseCode();
if(200 == responseCode){//表示服务端响应成功
//获取当前连接请求返回的数据流
InputStream is = connection.getInputStream();
InputStreamReader isr = new InputStreamReader(is, "UTF-8");
BufferedReader br = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String temp = null;
while(null != (temp = br.readLine())){
sb.append(temp);
}
/**
* 打印结果
*/
//response = sb.toString();
result = sb.toString();
System.out.println("返回XML:"+ sb.toString());
is.close();
isr.close();
br.close();
}
os.close();
return result;
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/hz/134025.html