Android request HTTP basic authentication

來源:互聯網
上載者:User
asp.net MVC web API實現了一個基於HTTP basic authentication身分識別驗證的RESTful實現。其中的AuthorizeAttribute實現為如下方式:
 1 public class HTTPBasicAuthorizeAttribute : System.Web.Http.AuthorizeAttribute 2     { 3         public override void OnAuthorization(System.Web.Http.Controllers.HttpActionContext actionContext) 4         { 5             if (actionContext.Request.Headers.Authorization != null) 6             { 7                 string userInfo = Encoding.Default.GetString(Convert.FromBase64String(actionContext.Request.Headers.Authorization.Parameter)); 8                 //使用者驗證邏輯 9                 if (string.Equals(userInfo, string.Format("{0}:{1}", "Hello", "123456")))10                 {11                     IsAuthorized(actionContext);12                 }13                 else14                 {15                     HandleUnauthorizedRequest(actionContext);16                 }17             }18             else19             {20                 HandleUnauthorizedRequest(actionContext);21             }22         }23 24         protected override void HandleUnauthorizedRequest(System.Web.Http.Controllers.HttpActionContext actionContext)25         {26             var challengeMessage = new System.Net.Http.HttpResponseMessage(System.Net.HttpStatusCode.Unauthorized);27             challengeMessage.Headers.Add("WWW-Authenticate", "Basic");28             throw new System.Web.Http.HttpResponseException(challengeMessage);29         }30     }

android作為用戶端調用此RESTful API需要如下實現:
 1 public static String invoke(String actionName) { 2         String result = null; 3         try { 4             String url = SERVER_URL + actionName + "/"; 5             Log.d(TAG, "url is" + url); 6  7             HttpGet httpReq = new HttpGet(url); 8             httpReq.addHeader(BasicScheme.authenticate( 9                     new UsernamePasswordCredentials("Hello", "123456"),10                     "UTF-8", false));11             DefaultHttpClient httpClient = new DefaultHttpClient();12             HttpResponse httpResponse = httpClient.execute(httpReq);13 14             StringBuilder builder = new StringBuilder();15             BufferedReader reader = new BufferedReader(new InputStreamReader(16                     httpResponse.getEntity().getContent()));17             for (String s = reader.readLine(); s != null; s = reader.readLine()) {18                 builder.append(s);19             }20             result = builder.toString();21             Log.d(TAG, "result is ( " + result + " )");22 23             // 儲存Cookie24             cookieStore = ((AbstractHttpClient) httpClient).getCookieStore();25         } catch (Exception e) {26             Log.e(TAG, e.toString());27         }28         Log.d(TAG, "over");29         return result;30     }

調用端需要注意的是需要將網站發布出去,android調用端要用192.168.1.100這樣的地址去訪問,一定不要用localhost這樣的地址,謹記!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.