隨着webService的崛起,我們開始中會越來越多的使用到訪問遠程webService服務。當然對於不同的webService框架一般都有自己的client包供使用,但是如果使用webService框架自己的client包,那麼必然需要在自己的代碼中引入它的包,如果同時調運了多個不同框架的webService,那麼就需要同時引入多個不同的client包,這樣做是很煩的。Java本生提供訪問遠程服務的包,在java.net.*下。接下來我們就用Java原生的package訪問webService。
1、發送GET請求
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
class ReadByGet implements Runnable
{ public void run()
{ try { URL
url = new URL( "http://fanyi.youdao.com/openapi.do?keyfrom=gusi123123&key=1075925116&type=data&doctype=json&version=1.1&q=hello" );//此處訪問有道的webService服務,參數都在url中 URLConnection
connection = url.openConnection(); //得到一個連接對象 InputStream
is = connection.getInputStream(); InputStreamReader
isr = new InputStreamReader(is, "UTF-8" ); BufferedReader
br = new BufferedReader(isr); //用於讀取返回的數據流 StringBuffer
sb = new StringBuffer(); //用於接收返回的數據 String
line = null ; while ((line
= br.readLine()) != null )
{ sb.append(line); } System.out.println(sb.toString()); br.close(); //關閉各種連接 isr.close(); is.close(); } catch (IOException
e) { e.printStackTrace(); } } } |
2、發送POST請求
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
class ReadByPost implements Runnable
{ public void run()
{ try { URL
url = new URL( "http://fanyi.youdao.com/openapi.do" );//此處通過post訪問有道webService //
URLConnection connection = url.openConnection(); HttpURLConnection
connection = (HttpURLConnection) url .openConnection(); //得到一個http的連接對象 connection.addRequestProperty( "encoding" , "UTF-8" ); connection.setRequestMethod( "POST" ); //設置請求方式 connection.setDoInput( true ); //設置可寫入 connection.setDoOutput( true ); //設置可讀取 OutputStream
os = connection.getOutputStream(); OutputStreamWriter
osw = new OutputStreamWriter(os, "UTF-8" ); BufferedWriter
bw = new BufferedWriter(osw); //定義寫入流對象 bw.write( "keyfrom=gusi123123&key=1075925116&type=data&doctype=xml&version=1.1&q=hello" ); bw.flush(); //通過寫入流對象寫入請求參數 InputStream
is = connection.getInputStream(); InputStreamReader
isr = new InputStreamReader(is, "UTF-8" ); BufferedReader
br = new BufferedReader(isr); //定義讀取流對象 StringBuffer
sb = new StringBuffer(); String
line = null ; while ((line
= br.readLine()) != null )
{ sb.append(line); } //通過讀取流讀取返回數據 //關閉各種連接對象 br.close(); isr.close(); is.close(); bw.close(); osw.close(); os.close(); System.out.println(sb.toString()); } catch (IOException
e) { e.printStackTrace(); } } } |
通過上面也發現使用java原生的package訪問webService比較麻煩,然後我們偉大的apache也給我們提供通用的訪問webService的jar包,需要的jar包依賴是:(使用到的類在org.apache.http.*)
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.6</version>
</dependency>
3、通過httpclient發送GET請求
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
public class ReadByClientGet implements Runnable
{ HttpClient
client = HttpClients.createDefault(); public void run()
{ String
uri = "http://www.baidu.com" ;//訪問百度 HttpGet
get = new HttpGet(uri); //得到請求對象 try { HttpResponse
response = client.execute(get); //執行訪問請求 HttpEntity
entity = response.getEntity(); //得到返回數據 String
result = EntityUtils.toString(entity, "UTF-8" ); //將返回數據輸出 System.out.println(result); } catch (ClientProtocolException
e) { e.printStackTrace(); } catch (IOException
e) { e.printStackTrace(); } } } |
4、通過httpclient發送POST請求
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
public class ReadByClientPost implements Runnable
{ HttpClient
client = HttpClients.createDefault(); //創建一個client對象 public void run()
{ try { String
uri = "http://fanyi.youdao.com/openapi.do" ;//post訪問有道webService HttpPost
post = new HttpPost(uri); //得到post請求對象 //構造請求參數,通過NameValuePair對象(類似於Map集合) List<NameValuePair>
parameters = new ArrayList<NameValuePair>(); parameters.add( new BasicNameValuePair( "keyfrom" , "gusi123123" )); parameters.add( new BasicNameValuePair( "key" , "1075925116" )); parameters.add( new BasicNameValuePair( "type" , "data" )); parameters.add( new BasicNameValuePair( "doctype" , "json" )); parameters.add( new BasicNameValuePair( "version" , "1.1" )); parameters.add( new BasicNameValuePair( "q" , "java" )); post.setEntity( new UrlEncodedFormEntity(parameters, "UTF-8" )); HttpResponse
response = client.execute(post); //執行post請求,並且接收返回數據 HttpEntity
entity = response.getEntity(); String
result = EntityUtils.toString(entity, "UTF-8" ); System.out.println(result); //輸出返回數據 } catch (ClientProtocolException
e) { e.printStackTrace(); } catch (IOException
e) { e.printStackTrace(); } } } |
通過上面httpclient只是最簡單的基本的訪問webService服務。當然還有很多其他細節以及功能可以設計和處理。接下來通過測試方法測試上面的四種訪問請求的結果:(輸出結果不再粘貼)
1
2
3
4
5
6
7
8
|
public class Main
{ public static void main(String[]
args) { //
new Thread(new ReadByGet()).start(); //
new Thread(new ReadByPost()).start(); //
new Thread(new ReadByClientGet()).start(); //new
Thread(new ReadByClientPost()).start(); } } |