`

URLconsction调用webservice

阅读更多
package com.cgm.urlws;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import sun.applet.Main;

public class App {

public static void main(String[] args) {

try {
URL  url=new URL("http://192.168.111.101:6789/hello?wsdl");
HttpURLConnection conn=(HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
OutputStream os=conn.getOutputStream();
String soap="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:q0=\"http://ws.cgm.com/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><soapenv:Body> <q0:sayHello><arg0>aaa</arg0> </q0:sayHello></soapenv:Body> </soapenv:Envelope>\"";
os.write(soap.getBytes());
InputStream is=conn.getInputStream();
String s="";
byte[] b=new byte[1024];
int len=0;
while ((len=is.read(b))!=-1) {
String ss=new String(b,0,len, "UTF-8");
s+=ss;
}
System.out.println(s);

//输出 <?xml version="1.0" ?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body><ns2:sayHelloResponse xmlns:ns2="http://ws.cgm.com/"><return>helloaaa</return></ns2:sayHelloResponse></S:Body></S:Envelope>

//通过xml解析出返回值



is.close();
os.close();
conn.disconnect();


} catch (Exception e) {
e.printStackTrace();
}


}

}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics