locked
Not able to call API from PCL code RRS feed

  • Question

  • User241356 posted

    In PCL I have the below code:

    public async Task MakePostRequest(string url, RegisterModel registerModel) { using (var client = new HttpClient()) { //url - "http://localhost:24929/api/Register/ParentRegister" var content = new StringContent(JsonConvert.SerializeObject(registerModel), Encoding.UTF8, "application/json"); var result = await client.PostAsync(url, content); return await result.Content.ReadAsStringAsync(); } }

    On button CLick in the activity I am calling this above method var res = await uReg.MakePostRequest(url, registerModel);

    Nothing happens. If I use fiddler and call that API its working .

    I am new to this, Please help me out of this .

    Wednesday, July 27, 2016 6:48 AM

Answers

  • User52634 posted

    @SiddharthKumar.1997,

    Since you are testing on the mobile and your server is running on your computer you should open your ip and ports for the mobile. Also instead of the localhost you should use your computer real ip e.g 192.168.0.10:24929

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Wednesday, July 27, 2016 7:52 AM
  • User52991 posted

    As Shalva says, you need to make sure your ports are open, if you're testing on a real device.

    But more importantly, you're saying "Nothing happens". What exactly do you mean by this? Absolutely nothing, or you're not getting the answer you expected? Because in any case, it shouldn't do 'nothing'. Worst case scenario (which is probably what's happening for you) it'll attempt to contact the IP you've specified, and after 30 seconds or so, will instead return a type of TimeOut exception.

    Try letting it run, and add a breakpoint to 'return await result.Content.ReadAsStringAsync();' and inspect 'result', see what it says for status code.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Wednesday, July 27, 2016 8:33 AM

All replies

  • User52634 posted

    @SiddharthKumar.1997,

    Since you are testing on the mobile and your server is running on your computer you should open your ip and ports for the mobile. Also instead of the localhost you should use your computer real ip e.g 192.168.0.10:24929

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Wednesday, July 27, 2016 7:52 AM
  • User52991 posted

    As Shalva says, you need to make sure your ports are open, if you're testing on a real device.

    But more importantly, you're saying "Nothing happens". What exactly do you mean by this? Absolutely nothing, or you're not getting the answer you expected? Because in any case, it shouldn't do 'nothing'. Worst case scenario (which is probably what's happening for you) it'll attempt to contact the IP you've specified, and after 30 seconds or so, will instead return a type of TimeOut exception.

    Try letting it run, and add a breakpoint to 'return await result.Content.ReadAsStringAsync();' and inspect 'result', see what it says for status code.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Wednesday, July 27, 2016 8:33 AM
  • User241356 posted

    Thank You @ashalva , will let you know once I have configured localhost for external applications access.

    @BjarkeSogaard , thank you for your suggestion will try that and let you know.

    Wednesday, July 27, 2016 10:42 AM
  • User241356 posted

    Finally its working now... thnk u so much both of u for ur quick response. @ashalva & @BjarkeSogaard

    Wednesday, July 27, 2016 12:10 PM