第一步:觀察一個網點java
http://www.webxml.com.cn/zh_cn/web_services.aspx?offset=1web
第二步:用java.net.Urlconnection向服務器發出請求服務器
UrlConnection低層是 Socket.app
public class Demo_Mobile { @Test public void test1() throws Exception{ URL url = new URL("http://webservice.webxml.com.cn" + "/WebServices/MobileCodeWS.asmx/" + "getMobileCodeInfo?mobileCode=" + "186982747585&userID="); //2:獲取鏈接 HttpURLConnection con = (HttpURLConnection) url.openConnection(); //3:設置鏈接超時時間 con.setConnectTimeout(3000); //設置爲get請求 con.setRequestMethod("GET"); //設置能夠從服務器讀取數據 con.setDoInput(true); //鏈接服務器 con.connect(); //判斷鏈接是否成功 int code = con.getResponseCode(); if(code==200){ //獲取服務器返回的數據 InputStream in = con.getInputStream(); StringBuilder sb = new StringBuilder(); byte[] bs = new byte[1024]; int len = 0; while((len=in.read(bs))!=-1){ sb.append(new String(bs,0,len)); } // System.err.println("返回的結果爲:"+sb.toString()); //解析 SAXReader sax = new SAXReader(); Document dom = sax.read(new StringReader(sb.toString())); Element root = dom.getRootElement(); String ss = root.getText(); System.err.println(ss); } //關閉鏈接 con.disconnect(); } }