问题描述:

I am executing the following java code to send XML request to webservice URL using java. I have looked at this link to create my script.

I am getting failure as the respone. I did a print of the XML string. XML is formed correctly with no issues.

I would need to know where I am going wrong and any guidance would be greatful at this time.

import java.net.*;

import java.io.*;

import java.security.*;

import javax.net.ssl.SSLSocketFactory;

import javax.net.ssl.SSLSocket;

import sun.net.www.http.HttpClient;

public class Sample {

public static void main(String[] args) throws Exception {

excutePost();

}

public static String excutePost() throws Exception {

URL url;

HttpURLConnection connection = null;

try {

//Create connection

String targetURL="https://XXXXXX.html";

System.out.println(targetURL);

String urlParameters="<?xml version=\"1.0\" encoding=\"iso-8859-1\"? >\r\n<request>\r\n <control>\r\n <senderid>XXXX</senderid>\r\n <password>XXXX</password>\r\n <controlid>ControlIdHere</controlid>\r\n <uniqueid>false</uniqueid>\r\n <dtdversion>3.0</dtdversion>\r\n </control>\r\n <operation>\r\n <authentication>\r\n <login>\r\n <userid>XXX</userid>\r\n <companyid>XXXX</companyid>\r\n <password>XXXX</password>\r\n </login>\r\n </authentication>\r\n\r\n<content>\r\n <function controlid=\"testControlId\">\r\n <readByQuery>\r\n <object>CUSTOMER</object>\r\n <query/>\r\n </readByQuery>\r\n </function>\r\n</content>\r\n </operation>\r\n</request>";

System.out.println(urlParameters);

url = new URL(targetURL);

connection = (HttpURLConnection)url.openConnection();

connection.setRequestMethod("POST");

connection.setRequestProperty("Content-Type",

"application/xml");

connection.setUseCaches (false);

connection.setDoInput(true);

connection.setDoOutput(true);

//Send request

DataOutputStream wr = new DataOutputStream (

connection.getOutputStream ());

wr.writeBytes (urlParameters);

wr.flush ();

wr.close ();

//Get Response

InputStream is = connection.getInputStream();

BufferedReader rd = new BufferedReader(new InputStreamReader(is));

String line;

StringBuffer response = new StringBuffer();

while((line = rd.readLine()) != null) {

response.append(line);

System.out.println(line);

response.append('\r');

}

rd.close();

return response.toString();

} catch (Exception e) {

e.printStackTrace();

return null;

} finally {

if(connection != null) {

connection.disconnect();

}

}

}

}

相关阅读:
Top