JAVA -get-post-soap方式反問外部webservices

xiaopengyaonixi發表於2016-11-08
package com.handkoo.webservices;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection; 
import java.net.URL;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;
 

  

public class MobileCodeServices {
	
	/**
	 * GET請求
	 * 
	 * @param mobilecode		mobilecode
	 * @param userID			使用者的編號
	 * @throws IOException		io異常
	 */
	public void get(String mobilecode,String userID) throws IOException{
		URL url = new URL("http://ws.webxml.com.cn//WebServices/MobileCodeWS.asmx/getMobileCodeInfo?mobileCode="
	     +mobilecode+"&userID="+userID);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setConnectTimeout(5000);
		conn.setRequestMethod("GET");
	    // 判斷是否請求成功
		if(conn.getResponseCode()==HttpURLConnection.HTTP_OK){
			// 獲取inputstream
			InputStream inputStream =conn.getInputStream();
			// 讀取記憶體
			ByteArrayOutputStream boas = new ByteArrayOutputStream();
			byte[] buffer = new byte[1024];
			int len = -1;
			while((len = inputStream.read(buffer))!=-1){
				boas.write(buffer, 0, len);
			}
			System.out.println("get請求獲取的資料:"+boas.toString("utf-8"));
			boas.close();
			inputStream.close();
		}
	}
	
	/**
	 * post請求
	 * @param mobileCode		手機號碼
	 * @param userId			使用者的編號
	 * @throws HttpException	http異常
	 * @throws IOException		io異常
	 */
	public void post(String mobileCode,String userId) throws HttpException, IOException{ 
		 HttpClient httpClient = new HttpClient();
		 PostMethod postMethod = new PostMethod("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo");
		 postMethod.setParameter("mobileCode", mobileCode);
		 postMethod.setParameter("userID", userId);
		 int code = httpClient.executeMethod(postMethod);
		 String result = postMethod.getResponseBodyAsString();
		 System.out.println("post請求的結果為:"+result); 
	}
	
	/**
	 * saop1 訪問方式
	 * @throws HttpException
	 * @throws IOException
	 */
	public void soap() throws HttpException, IOException{
		HttpClient client = new HttpClient();
		PostMethod postMethod = new PostMethod("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx");
		postMethod.setRequestBody(new FileInputStream("d://soap.xml"));
		postMethod.setRequestHeader("Content-Type","text/xml;charset=utf-8");	 
		int code = client.executeMethod(postMethod);
		System.out.println("結果碼:"+code);
		String reString = postMethod.getResponseBodyAsString();
		System.out.println("post請求的結果:"+reString);
	}
	
	
	public static void main(String[] args){
		MobileCodeServices mobileCodeServices = new MobileCodeServices();
		try {
			mobileCodeServices.get("xxxx", "");
			mobileCodeServices.post("xxxx", "");
			mobileCodeServices.soap();
		} catch (IOException e) {		 
			e.printStackTrace();
		}
	}
}

執行的結果:


相關文章