WebService SOAP、Restful和HTTP(post/get)請求區別


web service(SOAP)

Webservice的一個最基本的目的就是提供在各個不同平台的不同應用系統的協同工作能力。 
Web service 就是一個應用程序,它向外界暴露出一個能夠通過Web進行調用的API。 
SOAP是一種簡單基於xml的輕量協議,用戶web上交換結構化信息和類型信息。 
soap請求是HTTP POST的一個專用版本,遵循一種特殊的xml消息格式Content-type設置為: text/xml任何數據都可以xml化。 
本文將通過一個簡單的示例講解和演示Android平台的Web Service開發。

Ksoap2-android簡介

  在Android平台調用Web Service需要依賴於第三方類庫ksoap2,它是一個SOAP Web service客戶端開發包,主要用於資源受限制的Java環境如Applets或J2ME應用程序(CLDC/ CDC/MIDP)。認真讀完對ksoap2的介紹你會發現並沒有提及它應用於Android平台開發,沒錯,在Android平台中我們並不會直接使用ksoap2,而是使用ksoap2 android。KSoap2 Android 是Android平台上一個高效、輕量級的SOAP開發包,等同於Android平台上的KSoap2的移植版本。

  需要引入ksoap2-android-assembly-2.5.2-jar-with-dependencies.jar
//WebService的命名空間   static final String namespace = "http://impl.service.suncreate.com";   //服務器發布的url   static final String url = http://10.100.3.41/axis2/services/UploadService;   final String methodName = "upload"; // 函數名   final int sessionID = "111111"; //sessionID   //創建HttpTransportSE對象,通過HttpTransportSE類的構造方法可以指定WebService的url   HttpTransportSE transport = new HttpTransportSE(url);   transport.debug = true;   //指定WebService的命名空間和函數名   SoapObject soapObject = new SoapObject(namespace, methodName);   //設置調用方法參數的值   soapObject.addProperty("sessionID", sessionID); //sessionID   soapObject.addProperty("data", cds); //cds是需要傳遞的對象   SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);   envelope.bodyOut = transport;   envelope.setOutputSoapObject(soapObject);   //使用call方法調用WebService方法   transport.call(null, envelope);   SoapObject sb = (SoapObject) envelope.bodyIn;   String xmlMessage = sb.toString(); // 獲取從服務器端返回的XML字符串
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

Restful

REST(Representational State Transfer)一種輕量級的Web Service架構,可以完全通過HTTP協議實現。其實現和操作比SOAP和XML-RPC更為簡潔,還可以利用緩存Cache來提高響應速度,性能、效率和易用性上都優於SOAP協議。 
REST架構對資源的操作包括獲取、創建、修改和刪除資源的操作正好對應HTTP協議提供的GET、POST、PUT和DELETE方法(Verb)

Restful與SOAP的區別

安全性:SOAP會好於restful 
效率和易用性(REST更勝一籌) 
成熟度(總的來說SOAP在成熟度上優於REST)

HTTP-GET 和 HTTP-POST 
HTTP-GET和HTTP-POST是標准協議,他們使用HTTP(超文本傳輸協議)謂詞(謂詞是指條件表達式的求值返回真或假的過程。)對參數進行編碼並將參數作為名稱/值對傳遞,還使用關聯的請求語義。每個協議都包含一系列HTTP請求標頭,HTTP請求標頭及其他一些信息定義客戶端向服務器請求哪些內容,哪個服務器用一系列HTTP響應標頭和所請求的數據進行響應。

HTTP-GET 使用 MIME 類型 application/x-www-form-urlencoded(將追加到處理請求的服務器的 URL 中)以 URL 編碼文本的形式傳遞其參數。 URL 編碼是一種字符編碼形式,可確保傳遞的參數中包含一致性文本,例如將空格編碼為 %20,其它符號轉換為%XX,其中XX為該符號以16進制表示的ASCII(或ISO Latin-1)值。 追加的參數也稱為查詢字符串。

與 HTTP-GET 類似,HTTP-POST 參數也是 URL 編碼的。 但是,名稱/值對是在實際的 HTTP 請求消息內部傳遞的,而不是作為 URL 的一部分進行傳遞。 
我們日常網站、系統都是使用這種形式進行訪問我們的應用程序。

package cn.roco.manage.service; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; public class NewsService { public static final int POST = 1; public static final int GET = 2; public static final int HttpClientPost = 3; /** * 保存數據 * * @param title * 標題 * @param length * 時長 * @param flag * true則使用POST請求 false使用GET請求 * @return 是否保存成功 * @throws Exception */ public static boolean save(String path, String title, String timelength, int flag) throws Exception { Map<String, String> params = new HashMap<String, String>(); params.put("title", title); params.put("timelength", timelength); switch (flag) { case POST: return sendPOSTRequest(path, params, "UTF-8"); case GET: return sendGETRequest(path, params, "UTF-8"); case HttpClientPost: return sendHttpClientPOSTRequest(path, params, "UTF-8"); } return false; } /** * 通過HttpClient框架發送POST請求 * HttpClient該框架已經集成在android開發包中 * 個人認為此框架封裝了很多的工具類,性能比不上自己手寫的下面兩個方法 * 但是該方法可以提高程序員的開發速度,降低開發難度 * @param path * 請求路徑 * @param params * 請求參數 * @param encoding * 編碼 * @return 請求是否成功 * @throws Exception */ private static boolean sendHttpClientPOSTRequest(String path, Map<String, String> params, String encoding) throws Exception { List<NameValuePair> pairs = new ArrayList<NameValuePair>();// 存放請求參數 if (params != null && !params.isEmpty()) { for (Map.Entry<String, String> entry : params.entrySet()) { //BasicNameValuePair實現了NameValuePair接口 pairs.add(new BasicNameValuePair(entry.getKey(), entry .getValue())); } } UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pairs, encoding); //pairs:請求參數 encoding:編碼方式 HttpPost httpPost = new HttpPost(path); //path:請求路徑 httpPost.setEntity(entity); DefaultHttpClient client = new DefaultHttpClient(); //相當於瀏覽器 HttpResponse response = client.execute(httpPost); //相當於執行POST請求 //取得狀態行中的狀態碼 if (response.getStatusLine().getStatusCode() == 200) { return true; } return false; } /** * 發送POST請求 * * @param path * 請求路徑 * @param params * 請求參數 * @param encoding * 編碼 * @return 請求是否成功 * @throws Exception */ private static boolean sendPOSTRequest(String path, Map<String, String> params, String encoding) throws Exception { StringBuilder data = new StringBuilder(); if (params != null && !params.isEmpty()) { for (Map.Entry<String, String> entry : params.entrySet()) { data.append(entry.getKey()).append("="); data.append(URLEncoder.encode(entry.getValue(), encoding));// 編碼 data.append('&'); } data.deleteCharAt(data.length() - 1); } byte[] entity = data.toString().getBytes(); // 得到實體數據 HttpURLConnection connection = (HttpURLConnection) new URL(path) .openConnection(); connection.setConnectTimeout(5000); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setRequestProperty("Content-Length", String.valueOf(entity.length)); connection.setDoOutput(true);// 允許對外輸出數據 OutputStream outputStream = connection.getOutputStream(); outputStream.write(entity); if (connection.getResponseCode() == 200) { return true; } return false; } /** * 發送GET請求 * * @param path * 請求路徑 * @param params * 請求參數 * @param encoding * 編碼 * @return 請求是否成功 * @throws Exception */ private static boolean sendGETRequest(String path, Map<String, String> params, String encoding) throws Exception { StringBuilder url = new StringBuilder(path); url.append("?"); for (Map.Entry<String, String> entry : params.entrySet()) { url.append(entry.getKey()).append("="); url.append(URLEncoder.encode(entry.getValue(), encoding));// 編碼 url.append('&'); } url.deleteCharAt(url.length() - 1); HttpURLConnection connection = (HttpURLConnection) new URL( url.toString()).openConnection(); connection.setConnectTimeout(5000); connection.setRequestMethod("GET"); if (connection.getResponseCode() == 200) { return true; } return false; } } 
  • 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
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 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
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165

SOAP與HTTP的區別

大多數對外接口會實現web service方法而不是http方法。 c
web service相對http (post/get)的好處

1.接口中實現的方法和要求參數一目了然

2.不用擔心大小寫問題

3.不用擔心中文urlencode問題

4.代碼中不用多次聲明認證(賬號,密碼)參數

5.傳遞參數可以為數組,對象等…

6.web service相對http(post/get)由於要進行xml解析,速度可能會有所降低。

7.web service 完全可以可以被http(post/get)替代,而且現在的開放平台都是用的HTTP(post/get)實現的。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM