locked
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

  • Expose a public variable to keep the last HTTP error, and then you can call this from your component.

    Jeff Sanders (MSFT)

    @jsandersrocks - Windows Store Developer Solutions @WSDevSol
    Getting Started With Windows Azure Mobile Services development? Click here
    Getting Started With Windows Phone or Store app development? Click here
    My Team Blog: Windows Store & Phone Developer Solutions
    My Blog: Http Client Protocol Issues (and other fun stuff I support)

    Friday, January 10, 2014 2:10 PM
    Moderator
  • You might consider switching from the System.Net HttpClient to the new Windows.Web HttpClient -- it's got the same basic functionality, but integrates in with the regular web (technically, WinINet) cache, and has more options.  And you can use it from JavaScript, so you don't have to jump from one language to the other.
    Friday, January 10, 2014 7:25 PM
  • no. you need to upgrade your w8 app to 8.1 if want the new api.

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Monday, January 13, 2014 8:45 AM
  • On the "should I write to 8.1": yes, you should.  It's been well received and gets good reviews.  There are a bunch of new APis that "just work better" than the 8.0 versions.  If you do XAML in particular, I've liked working with the improved WebView control (indeed, my personal app-in-progress depends on the improvements to work).  And, of course, the view state bits changed; an 8.0 app won't have as flexible of a UI as an 8.0 app.


    Network Developer Experience Team (Microsoft)

    Monday, January 13, 2014 7:40 PM

All replies

  • How are you passing the cancellation token to cancel this?

    You have to provide a data member to store and return the http error.


    Jeff Sanders (MSFT)

    @jsandersrocks - Windows Store Developer Solutions @WSDevSol
    Getting Started With Windows Azure Mobile Services development? Click here
    Getting Started With Windows Phone or Store app development? Click here
    My Team Blog: Windows Store & Phone Developer Solutions
    My Blog: Http Client Protocol Issues (and other fun stuff I support)

    Thursday, January 09, 2014 1:16 PM
    Moderator
  • Thanks for your reply. Could you give me an example? I didn't quite understand your advice.
    Friday, January 10, 2014 12:51 AM
  • Expose a public variable to keep the last HTTP error, and then you can call this from your component.

    Jeff Sanders (MSFT)

    @jsandersrocks - Windows Store Developer Solutions @WSDevSol
    Getting Started With Windows Azure Mobile Services development? Click here
    Getting Started With Windows Phone or Store app development? Click here
    My Team Blog: Windows Store & Phone Developer Solutions
    My Blog: Http Client Protocol Issues (and other fun stuff I support)

    Friday, January 10, 2014 2:10 PM
    Moderator
  • You might consider switching from the System.Net HttpClient to the new Windows.Web HttpClient -- it's got the same basic functionality, but integrates in with the regular web (technically, WinINet) cache, and has more options.  And you can use it from JavaScript, so you don't have to jump from one language to the other.
    Friday, January 10, 2014 7:25 PM
  • Sorry for my belated reply. Windows.Web HttpClient seems to be the new feature in Windows 8.1 winmd. But I'm working on an win8 app. Could I use win8.1 winmd instead of win8's?
    Monday, January 13, 2014 7:47 AM
  • no. you need to upgrade your w8 app to 8.1 if want the new api.

    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    Monday, January 13, 2014 8:45 AM
  • On the "should I write to 8.1": yes, you should.  It's been well received and gets good reviews.  There are a bunch of new APis that "just work better" than the 8.0 versions.  If you do XAML in particular, I've liked working with the improved WebView control (indeed, my personal app-in-progress depends on the improvements to work).  And, of course, the view state bits changed; an 8.0 app won't have as flexible of a UI as an 8.0 app.


    Network Developer Experience Team (Microsoft)

    Monday, January 13, 2014 7:40 PM