c# - Portable Class Library HttpClient -



c# - Portable Class Library HttpClient -

for 1 of projects want develop library can used in different platforms (desktop, mobile, surface, etc). hence have opted porable class library.

i developing class calling different api calls' using httpclient. stuck how phone call method, response , work around. code :-

public static async task<jobject> executeget(string uri) { using (httpclient client = new httpclient()) { // todo - send http requests httprequestmessage reqmsg = new httprequestmessage(httpmethod.get, uri); reqmsg.headers.add(apiidtag, apiidkey); reqmsg.headers.add(apisecrettag, apisecret); reqmsg.headers.add("content-type", "text/json"); reqmsg.headers.add("accept", "application/json"); //response = await client.sendasync(reqmsg); //return response; //if (response.issuccessstatuscode) //{ string content = await response.content.readasstringasync(); homecoming (jobject.parse(content)); //} } } // perform agent login process public static bool agentstatus() { bool loginsuccess = false; seek { api_utility.executeget("http://api.mintchat.com/agent/autoonline").wait(); // access response, jobject ??? } grab { } { }

like executeget, create executepost. query executeget, if (1) pass jobject on parsing when issuccessstatuscode only, how can know other errors or messages inform user. (2) if pass response, how assign here

response = api_utility.executeget("http://api.mintchat.com/agent/autoonline").wait();

that giving error.

what best approach handle situation ? , got phone call multiple api's, different api have different result sets.

also, can confirm designing way , adding pcl reference able access in multiple projects.

update :- mentioned in below 2 answers have updated code. mentioned in provided link calling other project. code :-

portable class library :-

private static httprequestmessage getgetrequest(string url) { httprequestmessage reqmsg = new httprequestmessage(httpmethod.get, url); reqmsg.headers.add(apiidtag, apiidkey); reqmsg.headers.add(apisecrettag, apisecret); reqmsg.headers.add("content-type", "text/json"); reqmsg.headers.add("accept", "application/json"); homecoming reqmsg; } // perform agent login process public static async task<bool> agentstatus() { bool loginsuccess = false; httpclient client = null; httprequestmessage request = null; seek { client = new httpclient(); request = getgetrequest("http://api.mintchat.com/agent/autoonline"); response = await client.sendasync(request).configureawait(false); if (response.issuccessstatuscode) { string content = await response.content.readasstringasync().configureawait(false); jobject o = jobject.parse(content); bool stat = bool.parse(o["status"].tostring()); ///[mainappdataobject sharedappdataobject].authlogin.chatstatus = str; o = null; } loginsuccess = true; } grab { } { request = null; client = null; response = null; } homecoming loginsuccess; }

from other wpf project, in btn click event calling follows :-

private async void btnsignin_click(object sender, routedeventargs e) { /// other code goes here // .......... agent = dologin(emailid, encpswd); if (agent != null) { //agent.onlinestatus = getagentstatus(); // compile error @ line bool stat = await mintwinlib.helpers.api_utility.agentstatus(); ...

i these 4 errors :-

error 1 predefined type 'system.runtime.compilerservices.iasyncstatemachine' not defined or imported d:\...\mivechat\csc error 2 type 'system.threading.tasks.task`1<t0>' defined in assembly not referenced. must add together reference assembly 'system.threading.tasks, version=1.5.11.0, culture=neutral, publickeytoken=b03f5f7f89d50a3a'. d:\...\login form.xaml.cs 97 21 error 3 cannot find types required 'async' modifier. targeting wrong framework version, or missing reference assembly? d:\...\login form.xaml.cs 97 33 error 4 cannot find types required 'async' modifier. targeting wrong framework version, or missing reference assembly? d:\...\login form.xaml.cs 47 28

i tried adding system.threading.tasks pcl library only, gave 7 different errors. going wrong ? create working ?

please guide me on this. have spend lots of hours figuring best develop library accessible desktop app & win phone app. help highly appreciative. thanks.

if want result of task, need utilize result property:

var obj = api_utility.executeget("http://api.mintchat.com/agent/autoonline").result;

however, it's not thought wait synchronously async method complete, because can cause deadlocks. improve approach await method:

var obj = await api_utility.executeget("http://api.mintchat.com/agent/autoonline");

note need create calling method async well:

public static async task<bool> agentstatus()

sync , async code don't play well, async tends propagate across whole code base.

c# .net async-await dotnet-httpclient

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -