I want to find a 3-party accessing web service lib which using in a little sentence.
But the web service defindes it's custom AuthHeader object type. For passing the authentication info, it's not easy to find a path.
At the same time, I just want to test whether the web service is running correctly. Beacuse I use ksoap2-android lib to access it and it's hard to analyse the response...
Despite use 3-party lib to access web service, I just create a http requestion which accords with the SOAP requestion content.
The whole cords:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
public class SynDataFromWebservice {
public static void main(String[] args) {
String responseString = "";
String outputString = "";
String wsUrString = "http://192.168.1.11/GetDataService.asmx";
String METHODNAME = "GetDataSet";
String nameSpaceString = "http://www.domain.com/";
String soapActionString = "\"" + nameSpaceString+METHODNAME +"\"";
String oldTimeString = "1970-01-01T00:00:00.000Z";
URL url = null;
try {
url = new URL(wsUrString);
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
HttpURLConnection connection = null;
try {
connection = (HttpURLConnection)url.openConnection();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ByteArrayOutputStream bout = new ByteArrayOutputStream();
String xmlInputString =
"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
+"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""
+" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "
+" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
+" <soap:Header>"
+" <MyAuthHeader xmlns=\""+nameSpaceString+"\">"
+" <passWord>password</passWord>"
+" </MyAuthHeader>"
+" </soap:Header>"
+" <soap:Body>"
+" <"+METHODNAME+" xmlns=\""+nameSpaceString+"\">"
+" <oldTime>"+ oldTimeString +"</oldTime>"
+" </"+ METHODNAME +">"
+" </soap:Body>"
+"</soap:Envelope>";
byte[] buffer = new byte[xmlInputString.length()];
buffer = xmlInputString.getBytes();
try {
bout.write(buffer);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] b = bout.toByteArray();
connection.setRequestProperty("Host", "localhost");
connection.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
connection.setRequestProperty("Content-Length", String.valueOf(b.length));
connection.setRequestProperty("SOAPAction", soapActionString);
try {
connection.setRequestMethod("POST");
} catch (ProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
connection.setDoOutput(true);
connection.setDoInput(true);
OutputStream outputStream = null;
try {
outputStream = connection.getOutputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
outputStream.write(b);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
outputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
InputStreamReader reader = new InputStreamReader(connection.getInputStream(), "utf-8");
BufferedReader in = new BufferedReader(reader);
while((responseString = in.readLine()) != null){
outputString += responseString;
}
File saveFile = new File("e:\\data.xml");
if(saveFile.exists()){
saveFile.delete();
}else{
saveFile.createNewFile();
}
FileOutputStream fileOutputStream = new FileOutputStream(saveFile);
Writer writer = new BufferedWriter(new OutputStreamWriter(fileOutputStream, "utf-8"));
writer.write(outputString);
//fileOutputStream.write(outputString.getBytes("UTF-8"));
fileOutputStream.close();
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Links:
1.http://technology.amis.nl/2011/06/29/how-to-call-a-call-a-webservice-directly-from-java-without-webservice-library/;