Uncle also said Xamarin~android article ~ for httpclient sharing Session,android and API session sharing mechanism

Source: Internet
Author: User

Zatan

In the development of Android, our data is generally received through the interface, referring to the interface refers to the web Api,webservice,wcf,web applications, etc., they serve as the server and the database for direct communication, And the app this block by sending HTTP requests to these interfaces to obtain data, the advantage of the uncle thinks, can effectively reduce the software development difficulty, so the data interaction is separated to the service layer, and customer interaction is completely in the app side, which is similar to the current more popular SOA architecture, That is, a service for a variety of Terminal Services, whether your Web site, mobile phone iOS, mobile phone Android, tablet or other TV, and so on, all unified call the service layer interface!

It's a bit far, so let's look at how the session is shared with the Server API when HTTP is being sent to the app side

The principle is that we need to be clear.

-〉 Client

-〉 (Request) to access the server page

-〉 Server Generation SessionID

-〉 Storage to the server

-〉 (Response) corresponding to the client

-〉 client storage put SessionId into cookies (. NET platform cookies with the key named Asp.net_sessionid)

-〉 next request, the client will send the current SessionID to the server in the request header message.

-〉 service-side sessionid to maintain its effectiveness through expiration time

The code of practice comes from MSDN

Selected from: MSDN articles about HttpClient's cookiecontaineR

Uri uri =NewUri ("http://www.microsoft.com"); Httpclienthandler Handler=NewHttpclienthandler (); Handler. Cookiecontainer=NewCookiecontainer (); Handler. Cookiecontainer.add (URI,NewCookies ("name","value"));//Adding a CookieHttpClient client =NewHttpClient (handler); Httpresponsemessage Response=awaitclient.            Getasync (URI); Cookiecollection Collection= handler. Cookiecontainer.getcookies (URI);//Retrieving a Cookie
The code in the Uncle's project
Uri uri =NewUri (GetString (Resource.String.apiHost)); Httpclienthandler Handler=NewHttpclienthandler (); Handler. Cookiecontainer=NewCookiecontainer (); Handler. Cookiecontainer.add (URI,NewCookies ("Asp.net_sessionid", Inmemory.sessionid));//Adding a Cookie            using(varHTTP =NewHttpClient (handler)) {                varContent =NewFormurlencodedcontent (Newdictionary<string,string>() { }); varResponse = http. Postasync (GetString (Resource.String.apiHost) +"/test/currenttasklistapi", content); varobj = jsonconvert.deserializeobject<list<task_info>>(response. Result.Content.ReadAsStringAsync ().                Result); Listview.adapter=NewTask_infolistadapter ( This, obj); }

If you're using Xamarin to develop mobile apps, try it!

Finally, the uncle to say, the understanding of a concept determines the way you take the solution!

Uncle also said Xamarin~android article ~ for httpclient sharing Session,android and API session sharing mechanism

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.