package cn.it.ws.webservice; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.PostMethod; import java.io.IOException; public class phoneClientPost { public void GetRequest(String number) throws IOException { //創建瀏覽器 HttpClient httpClient = new HttpClient(); //填寫數據,發送get或者post請求 GetMethod getMethod = new GetMethod("http://ws.webxml.com.cn" + "/WebServices/MobileCodeWS.asmx/"+ "getMobileCodeInfo?mobileCode="+number+"&userID="); //發送請求,並用code接收返回狀態碼 int code = httpClient.executeMethod(getMethod); System.out.println("http:狀態碼為:"+code); //獲取返回結果 String result = getMethod.getResponseBodyAsString(); System.out.println("Get方法返回結果:"+result); } public void PostRequest(String number) throws IOException { //創建瀏覽器 HttpClient httpClient = new HttpClient(); //填寫數據,發送get或者post請求 PostMethod postMethod = new PostMethod("http://ws.webxml.com.cn" + "/WebServices/MobileCodeWS.asmx/getMobileCodeInfo"); postMethod.setParameter("mobileCode",number); postMethod.setParameter("userID",""); //發送請求,並用code接收返回狀態碼 int code = httpClient.executeMethod(postMethod); System.out.println("http:狀態碼為:"+code); //獲取返回結果 String result = postMethod.getResponseBodyAsString(); System.out.println("Post方法返回結果:"+result); } public static void main(String[] args) throws IOException { phoneClientPost client = new phoneClientPost(); client.PostRequest("134288888888"); } }