webservice调用方式:

1.httpget

2.httpost

3.httpsoap

其中soap的优点是可以传递结构化的数据,前两者不行。

 

webservice的通讯原理是其底层也是通过http传输的。所以手动连接webservice server有两种方式。

第一种是使用jaxm


 

这种方法比较复杂,使用soapconnection

public static void post2WebService(String content, String url) {

try {

// First create the connection
SOAPConnectionFactory soapConnFactory = SOAPConnectionFactory
.newInstance();
SOAPConnection connection = soapConnFactory.createConnection();

// Next, create the actual message
MessageFactory messageFactory = MessageFactory.newInstance();
SOAPMessage message = messageFactory.createMessage();

// Create objects for the message parts
SOAPPart soapPart = message.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
SOAPBody body = envelope.getBody();

// String to inputstream
InputStream inputstream = new ByteArrayInputStream(
content.getBytes("UTF-8"));
// Populate the Message
StreamSource preppedMsgSrc = new StreamSource(inputstream);
soapPart.setContent(preppedMsgSrc);
// Save the message
message.saveChanges();

// Send the message and get a reply

// Set the destination
URLEndpoint destination = new URLEndpoint(url);
// Send the message
SOAPMessage reply = connection.call(message, destination);

// Create the transformer
TransformerFactory transformerFactory = TransformerFactory
.newInstance();
Transformer transformer = transformerFactory.newTransformer();
// Extract the content of the reply
Source sourceContent = reply.getSOAPPart().getContent();

// Set the output for the transformation
StringWriter writer = new StringWriter();
// StreamResult result = new StreamResult(System.out);
StreamResult result = new StreamResult(writer);
transformer.transform(sourceContent, result);
System.out.println("result : " + writer.toString());

// Close the connection
connection.close();

} catch (Exception e) {
LOG.error("post2WebService error : " + e.getMessage());
}

}

 

此处是一小段代码参考,如何将streamResult转换为String的

try {
StreamSource source = new StreamSource(new StringReader("<xml>blabla</xml>"));
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
transformer.transform(source,result);
String strResult = writer.toString();
} catch (Exception e) {
e.printStackTrace();
}

这里还需要使用到jaxm-api.jar

 

第一种方法较为复杂,现在介绍第二种方法,直接使用httpConnection post message

public static String sendPost2WS(String url, String param) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
//open url connect
URLConnection conn = realUrl.openConnection();
//set common properties
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
//send POST request must set 2 lins below
conn.setDoOutput(true);
conn.setDoInput(true);
//get URLConnection object out put stream
out = new PrintWriter(conn.getOutputStream());
//send request parameters
out.print(param);
// flush out put stream cache
out.flush();
//define BufferedReader to get the URL response
in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("send POST has error : "+e.getMessage());
e.printStackTrace();
}
//close in & out put stream
finally{
try{
if(out!=null){
out.close();
}
if(in!=null){
in.close();
}
}
catch(IOException ex){
ex.printStackTrace();
}
}
return result;
}