package com.testweb.urlinfo;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class TestWebService {
/**
* @param args
*/
public static void main(String[] args) {
try{
URL urlUrl = new URL("http://192.168.1.123:8880/Service.asmx");// WebService 地址
HttpURLConnection urlConn = (HttpURLConnection) urlUrl.openConnection();
urlConn.setDoOutput(true);
urlConn.setDoInput(true);
urlConn.setConnectTimeout(12000); //连接时间
urlConn.setReadTimeout(12000);
urlConn.setRequestMethod("POST");
urlConn.setUseCaches(false);
urlConn.setRequestProperty("Content-Type","text/xml; charset=utf-8"); //这里要根据你的 webservice 配置
urlConn.connect();
DataOutputStream out =new DataOutputStream(urlConn.getOutputStream());
/**
* WebService 提供的访问格式,用浏览器打开 WebService 地址 选择某个方法,
* 会有相应的访问请求信息,拷到里面就可以了,注意在每个双冒号前加 '\'
* 这里是请求内容,要获取的内容是: TestInfo
* **/
String content="<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:Body> <TestInfo xmlns=\"http://www.testweb.com/\"> <strValue>TestText</strValue> </TestInfo> </soap:Body></soap:Envelope>";
out.writeBytes(content);
out.flush();
out.close();
/**
* 这是响应的内容
* */
BufferedReader reader=new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
String line;
StringBuffer strBuffer = new StringBuffer();
while ((line = reader.readLine()) != null) {
strBuffer.append(line); //这就是得到的全部内容,具体内容怎么获取出来自己写吧。
System.out.println("Get WebService >" + line);
}
reader.close();
urlConn.disconnect();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}