我正在尝试使用以下代码请求网络服务
HttpURLConnection conn = null;
String operation = "validateAddress";
String urlStr = "http://.....";
String requestXML = "<soapenv:Envelope "
+ "xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" "
+ "xmlns:v3=\"http://cio.att.com/commonheader/v3\" "
+ "xmlns:loc=\"http://ovalsgis.ebm.att.com/locationgeocoderservicerequest.xsd\">"
+ "<soapenv:Body>"
+ "<loc:validateGisAddressRequest "
+ "requestFlag=\"F\" "
+ "telcoFlag=\"N\">"
+ "<loc:FieldedAddressInfo>"
+ " <loc:houseNumber>10</loc:houseNumber> "
+ " <loc:streetName>E MURPHY AVE</loc:streetName> "
+ " <loc:city>SAPULPA</loc:city> "
+ " <loc:state>OK</loc:state> "
+ " <loc:country>USA</loc:country>"
+ "</loc:FieldedAddressInfo>"
+ "</loc:validateGisAddressRequest>"
+ "</soapenv:Body>"
+ "</soapenv:Envelope>";
byte[] reqload = requestXML.getBytes();
URL server = new URL(urlStr.trim());
conn = (HttpURLConnection) server.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setAllowUserInteraction(false);
conn.setRequestProperty("Username", "abcTest");
conn.setRequestProperty("Password", "abcPassword");
conn.setRequestProperty("WSS-Password Type", "PasswordText");
conn.setRequestProperty("Content-Type", "text/xml");
String reqLen = Integer.toString(reqload.length);
conn.setRequestProperty("Content-Length", reqLen);
conn.setRequestProperty("SOAPAction", operation);
OutputStream os = conn.getOutputStream();
os.write(reqload);
os.flush();
logger.info("Received response Code (" + conn.getResponseCode() + ")");
但我得到以下回应:
错误请求 - 标头无效 HTTP错误400.请求具有无效的标头名称
任何人都可以帮我吗?
答案 0 :(得分:0)
您应该使用javax.xml.soap
发出SAOP请求。
请查看documentation。
此外,this answer可能会有所帮助。
答案 1 :(得分:0)
@fateddy
更新的代码:
public class testWS {
public static void main(String args[]) throws Exception{
HttpURLConnection conn = null;
String operation = "validateGisAddress";
String url = "http://.......";
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);
printSOAPResponse(soapResponse);
soapConnection.close();
}
private static SOAPMessage createSOAPRequest() throws Exception {
String requestXML = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:v3=\"http://cio.att.com/commonheader/v3\" xmlns:loc=\"http://ovalsgis.ebm.att.com/locationgeocoderservicerequest.xsd\"><soapenv:Body><loc:validateGisAddressRequest requestFlag=\"F\" telcoFlag=\"N\" username=\"?\" altLocationPropertiesIndicator=\"?\" superScore=\"?\"><!--You have a CHOICE of the next 2 items at this level--><loc:FieldedAddressInfo><!--Optional:--> <loc:houseNumber>10</loc:houseNumber> <loc:streetName>E MURPHY AVE</loc:streetName> <loc:city>SAPULPA</loc:city> <loc:state>OK</loc:state> <loc:country>USA</loc:country></loc:FieldedAddressInfo></loc:validateGisAddressRequest></soapenv:Body></soapenv:Envelope>";
InputStream is = new ByteArrayInputStream(requestXML.getBytes());
SOAPMessage soapMessage = MessageFactory.newInstance().createMessage(null, is);
String authorization = new sun.misc.BASE64Encoder().encode(("abcTest:abcPassword").getBytes());
MimeHeaders hd = soapMessage.getMimeHeaders();
hd.addHeader("Authorization", "Basic " + authorization);
soapMessage.saveChanges();
soapMessage.writeTo(System.out);
return soapMessage;
}
private static void printSOAPResponse(SOAPMessage soapResponse) throws Exception {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
Source sourceContent = soapResponse.getSOAPPart().getContent();
System.out.print("\nResponse SOAP Message = ");
StreamResult result = new StreamResult(System.out);
transformer.transform(sourceContent, result);
}
}