REST風格WebService呼叫客戶端
1. 客戶端介面
package com.http.client; /** * * Http客戶端介面 * @author ypqiao * */ public interface HttpClient { /** 傳送GET請求,返回 文字資料 **/ public abstract String get(String urlStr) throws Exception; /** 傳送GET請求,返回二進位制資料 **/ public abstract byte[] getByte(String urlStr) throws Exception; /** 傳送POST請求,返回文字資料 **/ public abstract String post(String urlStr, String params) throws Exception; }
2. 客戶端實現
package com.http.client; import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; /** * * Http客戶端實現 * @author ypqiao * */ public class HttpClientImpl implements HttpClient { protected int cach_size = 3*1024*1024; protected int con_timeout = 36000; protected int read_timeout = 30000; protected String charset = "UTF-8"; public HttpClientImpl() {} /* (non-Javadoc) * @see com.http.client.HttpClient#get(java.lang.String) */ @Override public String get(String urlStr) throws Exception{ String response = null; URL url = null; HttpURLConnection con = null; BufferedReader ins = null; url = new URL(urlStr); con = (HttpURLConnection) url.openConnection(); con.setConnectTimeout(con_timeout); con.setReadTimeout(read_timeout); con.connect(); ins = new BufferedReader( new InputStreamReader(con.getInputStream()),cach_size); if( con.getResponseCode() == 200 ){ String line = null; StringBuilder rspStr = new StringBuilder(); while( (line = ins.readLine()) != null ){ rspStr.append(line); } response = rspStr.toString(); } else { throw new HttpCommunicationException(con.getResponseCode(), con.getResponseMessage()); } ins.close(); con.disconnect(); return response; } /* (non-Javadoc) * @see com.http.client.HttpClient#post(java.lang.String, java.lang.String) */ @Override public String post(String urlStr,String params ) throws Exception { String response = null; URL url = null; HttpURLConnection con = null; BufferedReader ins = null; OutputStream ous = null; url = new URL(urlStr); con = (HttpURLConnection) url.openConnection(); con.setConnectTimeout(con_timeout); con.setReadTimeout(read_timeout); con.setDoInput(true); con.setDoOutput(true); con.setRequestMethod("POST"); con.setUseCaches(false); con.connect(); ous = con.getOutputStream(); ous.write(params.getBytes(charset)); ous.flush(); ous.close(); ins = new BufferedReader( new InputStreamReader(con.getInputStream()),cach_size); if( con.getResponseCode() == 200 ){ String line = null; StringBuilder rspStr = new StringBuilder(); while( (line = ins.readLine()) != null ){ rspStr.append(line); } response = rspStr.toString(); } else { throw new HttpCommunicationException(con.getResponseCode(), con.getResponseMessage()); } ins.close(); con.disconnect(); return response; } /* (non-Javadoc) * @see com.http.client.HttpClient#getByte(java.lang.String) */ @Override public byte[] getByte(String urlStr) throws Exception { byte[] response = null; URL url = null; HttpURLConnection con = null; BufferedInputStream ins = null; url = new URL(urlStr); con = (HttpURLConnection) url.openConnection(); con.setConnectTimeout(con_timeout); con.setReadTimeout(read_timeout); con.connect(); ins = new BufferedInputStream(con.getInputStream(),cach_size); if( con.getResponseCode() == 200 ){ int b = 0; int index = 0; byte[] response_tmp = null; response = new byte[cach_size]; while( (b=ins.read()) != -1 ){ if( index > response.length - 1){ response_tmp = response; response = new byte[response_tmp.length*2]; for( int i=0; i<response_tmp.length; i++){ response[i] = response_tmp[i]; } } response[index] = (byte)b; index++; } response_tmp = response; response = new byte[index]; for( int i=0; i<index; i++){ response[i] = response_tmp[i]; } } else { throw new HttpCommunicationException(con.getResponseCode(), con.getResponseMessage()); } ins.close(); con.disconnect(); return response; } }
3. 客戶端異常
package com.http.client; /** * * Http執行時通訊異常 * @author ypqiao * */ public class HttpCommunicationException extends RuntimeException { private static final long serialVersionUID = 1L; private int code; private String msg; public HttpCommunicationException( int code,String msg){ this.code = code; this.msg = msg; } @Override public String toString() { return "返回碼:"+code+"訊息描述:"+msg; } public int getCode() { return code; } public void setCode(int code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } }
4.測試
package com.http.client;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
/**
*
* Http客戶端測試類
* @author ypqiao
*
*/
public class RestTest {
public static void main(String[] args) throws Exception {
/** 測試方法為呼叫REST風格的WebService **/
HttpClient httpClient = new HttpClientImpl();
// 發GET請求,返回文字資料(html/xml)
System.out.println(httpClient.get("http://server.arcgisonline.com/arcgis/rest/services"));
// 發GET請求,返回文字資料(json)
System.out.println(httpClient.get("http://server.arcgisonline.com/arcgis/rest/services?f=json"));
// 傳送GET請求,返回二進位制資料(image),存放路徑為c:/
byte[] bytes = httpClient.getByte("http://sampleserver1c.arcgisonline.com/arcgisoutput/_ags_map5e57267ff6fb4227a8f8685915856213.png");
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream( new File("c:/export.png")));
out.write(bytes);
out.flush();
out.close();
// 傳送POST請求,返回文字資料
System.out.println(httpClient.post("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StateCityHighway_USA/MapServer/find", "searchText=New York&layers=1"));
}
}
相關推薦
REST風格WebService呼叫客戶端
1. 客戶端介面 package com.http.client; /** * * Http客戶端介面 * @author ypqiao * */ public interface HttpClient { /** 傳送GET請求,返回 文字資料 **/
gsoap生成webservice呼叫客戶端介面
1.下載gsoap2.8 2.執行 wsdl2h.exe -o XXX.h XXX.wsdlwsdl檔案可以是本地檔案,也可以是伺服器的wsdl,比如http://192.168.0.122:3333/ws/uss?wsdl 3.生成客戶端程式碼 soapcpp2.ex
利用Axis2開發WebService(3)---用Java實現呼叫WebService的客戶端程式
WebService是為程式服務的,只在瀏覽器中訪問WebService是沒有意義的。因此,在本節使用Java實現了一個控制檯程式來呼叫上一節釋出的WebService。呼叫WebService的客戶端程式碼如下: package client; impor
webservice公共呼叫 客戶端介面工具類 cfx axis2
package com.senyint.util; import java.rmi.RemoteException; import javax.xml.namespace.QName; import javax.xml.rpc.ParameterMode;
CXF開發webservice需要的最少jar包,CXF釋出和呼叫客戶端程式碼
簡單介紹一下cxf開發客戶端和服務端的程式碼塊 釋出服務端程式碼: Java程式碼 @WebService publicinterface ApprovalService { /** * 3.1.10. 審批結果回撥介面 * @param status
呼叫WebService服務客戶端程式碼編寫
public class AxisClientSample { public static void main(String[] args) throws Exception { String[] recipients = new String[]{}; //收件人 String strSubje
用eclipse呼叫遠端webservice生成客戶端程式碼
以前在呼叫webservice的時候都是自己老老實實用axis寫程式碼,今天在網上看到在myeclipse裡面可以根據wsdl介面地址自動生成介面呼叫客戶端程式碼,於是我就想到在eclipse裡面是不是也可以根據wsdl介面地址自動生成介面客戶端呼叫程式碼呢?答案是肯定的,
java 呼叫webservice (asmx) 客戶端開發示例
String inputParam = "測試";Service service = new Service(); String url = "http://xxxxxxx/service/getinfo.asmx"; //URL地址String namespace = "http://tempuri.o
EJB3 釋出WebService及客戶端呼叫
EJB3釋出WebService很簡單,但是在客戶端呼叫上卻遇到了太多的問題,差不多一天的時間終於搞定了,下面是整個過程,我的伺服器採用Weblogic10.3 釋出WebService /** * @author 王碩 **/
[Webservice] Eclipse根據wsdl檔案自動生成webservice的呼叫客戶端
生成客戶端: 1. 帶有webservice外掛的Eclipse工具; 2. 首先用瀏覽器訪問webservice的站點,接著儲存開啟的頁面字尾為wsdl; 3. 在Eclipse中生成webservice客戶端程式碼,New---->Other----&
SpringBoot整合cxf發布webService和客戶端的調用
wire 註解 pac point login isp 3.1 component amp SpringBoot整合cxf發布webService 1. 看看項目結構圖 2. cxf的pom依賴 1 <dependency> 2 <grou
專案總結:每隔5分鐘從資料庫拉取資料轉為Json格式通過WebService從客戶端傳送至服務端
第一次接手需求寫的小專案,過程也有點坎坷,以此記錄總結編碼過程中遇到的問題。 專案背景:本次寫的是一個小模組,主要為客戶端,作用是每隔5分鐘從資料庫拉取資料傳送至服務端。本次專案採用的是spring3+Quartz+JdbcTemplate+J
整合XFire與Spring, 釋出 Webservice 以及客戶端的訪問方式
首先第一步當然是先下載好服務端的依賴了; 服務端依賴包 新建Web工程,為了後續的客戶端的測試,還需加入commons- httpclient.jar包到WEB-INF/lib下哦; 在web.xm
webservice RPC客戶端
class 1 import javax.xml.namespace.QName; import org.apache.axiom.om.OMElement; import org.apache.axis2.addressing.EndpointReference; imp
webservice java客戶端設定超時時間
//webservice例項 moreLikeThisHBaseWebServiceServiceStub = new MoreLikeThisHBaseWebServiceServiceStub(url); //設定超時時間 Options options = moreLikeThisHBaseW
SpringCloud學習 - Feign宣告式服務呼叫客戶端//介面方式
Feign宣告式服務呼叫客戶端//介面方式 書籤: 1、小小例子 2、Get多個請求引數處理 小小例子 新建專案:eureka-consumer-feign pom核心配置: <dependency&
webservice 生成客戶端程式碼
命令如下: wsimport -keep -d d:\ -s d:\src -p com.hello -verbose http://127.0.0.1:9999/hello?wsdl -d:指定class檔案的存放目錄 -s:指定原始碼java輸出目錄 -p:以pac
Eclipse根據wsdl檔案生成webService(cxf)客戶端(二)
cxf webservice服務端:http://blog.csdn.net/huzheaccp/article/details/8742803 cxf webservice服務端:http://blog.csdn.net/huzheaccp/article/details
web呼叫客戶端程式
背景 最近做一個整合需求,我們是B/S架構的,對方是C/S架構的,對方直接扔過來一個EXE連OCX都沒有,讓我們呼叫,也就是說,我們需要通過js程式去呼叫他們的客戶端程式並傳入多個引數,當時內心是崩潰的,網上查了些資料,發現還真的可以!下面開始。 原理 1.在登錄檔中把需要
php webservice實現客戶端提交資料庫資料到伺服器並返回另一份資料庫資料
由於公司需求,需使用webservice來開發公司erp的伺服器和客戶端的兩邊資料庫交換。 即每次把客戶端更新的資料上傳到生產用的伺服器端並把伺服器端剛更新的資料返回回來。(伺服器端有指令碼在執行更新資料) 由於使用的是php語言,當前網路上大部分解決上傳問題的都是jav