0

I am trying to request a web service using the below code

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() + ")");

But i am getting the following in response:

Bad Request - Invalid Header HTTP Error 400. The request has an invalid header name

Can anyone help me with this?

nikita
  • 13
  • 7
  • Your request could not be understood by the endpoint. Use SoapUI to test the service. And for the implementation you should use a SOAP library - explained here: http://stackoverflow.com/questions/15940234/how-to-do-a-soap-web-service-call-from-java-class – fateddy Jan 04 '16 at 13:15
  • @fateddy Thanks for your reply. I forgot to mention it in the post that i have already tested it with SoapUI and it works well with it. I am trying with the above mentioned SAAJ library but unable to pass the authentication details.Can you please with help me with it? – nikita Jan 04 '16 at 14:21
  • Could you please update your code (where you are using saaj)? And please post the wsdl as well. – fateddy Jan 04 '16 at 17:30

2 Answers2

0

You should use javax.xml.soap to make a SAOP request. Please have a look to the documentation.

Also, this answer might be helpful.

Community
  • 1
  • 1
Felix
  • 17
  • 6
0

@fateddy

Updated code:

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);
}

}

nikita
  • 13
  • 7
  • you should edit your question rather than providing the update as an answer. could you please do that (and delete this answer)? and also please add the wsdl. – fateddy Jan 05 '16 at 08:12