none
HTTP request of C# component problems

    Question

  • I used WinJS.xhr() to fetch data from Webservice before. But a new requirement is to hide the request urls, so I use System.Net.Http.HttpClient instead. Code like this:

    public static IAsyncOperation<string> requestData(string url, string param) { return AsyncInfo.Run((CancellationToken ct) => Task.Run<string>(async() => {

    HttpClient client = new HttpClient(); HttpContent httpContent = new StringContent(param); httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/x-www-form-urlencoded"); var response = await client.PostAsync(url, httpContent); return response.Content.ReadAsStringAsync().Result;

    })); }

    var req; function request(url, param){ if(req) req.cancel(); req = Query.Data.requestData(url,param).then(function(data){ ... req = null; }, function(e){

    console.error(e.message);

    req = null;

    }); }

    I've got 2 problems.

    a) It couldn't be cancelled correctly in js.

    b) I want handle different status of http request error. But I couldn't get any useful information in requestData error handle.  How to get the http error code from C# component.

    Thank you for your attention.^_^


    Thursday, January 09, 2014 7:23 AM

Answers

All replies