跟发送XML报文的请求的唯一差别:
httpConn.setRequestProperty("SOAPAction","urn:action");//添加了该请求头
至于该属性的值是多少,你们可以自行用SOAPUI工具(软件怎么使用就自行百度吧)去发送SOAP请求,看它具体发送成功的请求头信息,请求日志如下为:
请求的代码如下:
/**
* 发送POST请求的SOAP报文格式
* @param SOAPUrl 请求地址
* @param parXmlInfo 请求报文
* @return
*/
public static String SendSoapPost(String SOAPUrl,String parXmlInfo){
StringBuilder result = new StringBuilder();
OutputStream out = null;
BufferedReader in = null;
try {
URL url = new URL(SOAPUrl);
URLConnection connection = url.openConnection();
HttpURLConnection httpConn = (HttpURLConnection) connection;
byte[] b = parXmlInfo.getBytes("ISO-8859-1");
httpConn.setRequestProperty( "Content-Length",String.valueOf( b.length ) );
httpConn.setRequestProperty("Content-Type","text/xml; charset=utf-8");
httpConn.setRequestProperty("SOAPAction","urn:action");//重点中的重点,不加就500
httpConn.setRequestMethod( "POST" );
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
out = httpConn.getOutputStream();
out.write( b );
in = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
result.append(inputLine);
out.close();
in.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try{
if(out!=null){
out.close();
}
if(in!=null){
in.close();
}
}
catch(IOException ex){
ex.printStackTrace();
}
}
System.out.println("\n respone:\n"+result);
return result.toString();
}