尝试像这样发送请求。这就是我上次做的方式:
try
{
StringBuffer strBuffer = new StringBuffer();
HttpURLConnection connection = connectToEndPoint(endpoint);
OutputStream outputStream = generateXMLOutput(connection, yourvalue, strDate);
InputStream inputStream = connection.getInputStream();
int i;
while ((i = inputStream.read()) != -1) {
Writer writer = new StringWriter();
writer.write(i);
strBuffer.append(writer.toString());
String status = xmlOutputParse(strBuffer);
使用的函数:
private static HttpURLConnection connectToEndPoint(String wsEndPoint) throws MalformedURLException, IOException {
URL urlEndPoint = new URL(wsEndPoint);
URLConnection urlEndPointConnection = urlEndPoint.openConnection();
HttpURLConnection httpUrlconnection = (HttpURLConnection) urlEndPointConnection;
httpUrlconnection.setDoOutput(true);
httpUrlconnection.setDoInput(true);
httpUrlconnection.setRequestMethod("POST");
httpUrlconnection.setRequestProperty("content-type", "application/soap+xml;charset=UTF-8");
System.setProperty("sun.net.client.defaultConnectTimeout", String.valueOf(2 * 1000));
System.setProperty("sun.net.client.defaultReadTimeout", String.valueOf(2 * 1000));
return httpUrlconnection;
}
你需要手动创建XML文档(根据你的需求进行修改):
private static OutputStream generateXMLOutput(HttpURLConnection conn, String msisdn, String strDate) throws IOException {
OutputStream outputStream = conn.getOutputStream();
StringBuffer buf = new StringBuffer();
buf.append("<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:ins=\"http://yournamespace">\r\n");
buf.append("<soap:Header xmlns:wsa=\"http://www.w3.org/2005/08/addressing\">\r\n");
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream, "UTF-8");
outputStreamWriter.write("<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:ins=\"http://yournamespace\">\r\n");
outputStreamWriter.write("<soap:Header xmlns:wsa=\"http://www.w3.org/2005/08/addressing\">\r\n");
outputStreamWriter.flush();
outputStream.close();
return outputStream;
}
并且这个函数将返回你的WS答案:
private static String xmlOutputParse(StringBuffer xmlInputParam) throws IOException, ParserConfigurationException, SAXException {
String status = null;
DocumentBuilderFactory docBuilderfFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = docBuilderfFactory.newDocumentBuilder();
InputSource inputSource = new InputSource();
inputSource.setCharacterStream(new StringReader(xmlInputParam.toString()));
Document document = documentBuilder.parse(inputSource);
NodeList nodeList = document.getElementsByTagName("ResponseHeader");
Element element = (Element) nodeList.item(0);
if (element == null) {
return null;
}
NodeList name = element.getElementsByTagName("Status");
Element line = (Element) name.item(0);
if (line == null) {
return null;
}
if (line.getFirstChild() instanceof CharacterData) {
CharacterData cd = (CharacterData) line.getFirstChild();
status = cd.getData().trim();
}
return status;
}
我认为这个解决方案(虽然有点长)适用于大多数情况。希望你能根据自己的需求进行调整。
最好的问候!
xml here
,会发生什么? - brimborium