1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
public String sendRequest() {
String outputResponse = "";
try {
URL url= new URL(WebServicePath);
System.out.println("\nWeb Server:");
System.out.println(WebServicePath);
// open an HTTP connection to the web service
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setDoInput(true);
// make it a SOAP request
connection.setRequestProperty("Content-type", "text/xml; charset=utf-8");
connection.setRequestProperty("SOAPAction", XmlNamespace + MethodName);
// build the SOAP request for GetTime
//http://localhost/time/TimeService.asmx?op=GetTime
String strParameters = "";
for (int t = 0; t < ParamNames.size(); t++) {
String name = (String) ParamNames.elementAt(t);
String data = (String) ParamData.elementAt(t);
strParameters = strParameters + "<" + name + ">" + data + "</" + name + ">\n";
}
String msg =
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
"<soap:Envelope " +
//" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
//" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n" +
" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
" <soap:Body>\n" +
" <" + MethodName + " xmlns=\"" + XmlNamespace +"\">\n" +
strParameters +
" </"+ MethodName +"> \n" +
" </soap:Body>\n" +
"</soap:Envelope>";
// send the SOAP request
byte[] bytes = msg.getBytes();
connection.setRequestProperty("Content-length", String.valueOf(bytes.length));
// output
System.out.println("\nSOAP call:");
System.out.println("Content-type:"+connection.getRequestProperty("Content-type"));
System.out.println("Content-length:"+connection.getRequestProperty("Content-length"));
System.out.println("SOAPAction:"+connection.getRequestProperty("SOAPAction"));
System.out.println(msg);
OutputStream out = connection.getOutputStream();
out.write(bytes);
out.close();
// read and print the SOAP response
BufferedReader in = new BufferedReader(
new InputStreamReader(
connection.getInputStream()));
String inputLine;
System.out.println("\nServer response:");
while ((inputLine = in.readLine()) != null)
{
System.out.println(inputLine);
outputResponse+=inputLine;
}
in.close();
} catch (Exception e) {
System.out.println("-- error:" + e.getMessage());
return null;
}
return outputResponse;
} |
Partager