locked
UWP and HttpClient RRS feed

  • Question

  • Here is my C#

    var client = new HttpClient();
    var url = "http://" + Ip + "/GET NOWORNEXT EPG { \"count\" : \"30\", \"group\" : 6, \"epgNowOrNextFlag\" : \"1\", \"startIdx\" : \"0\" } ";
    var response = await client.GetAsync(url);
    var result = await response.Content.ReadAsStringAsync();

    This code is working on PC, also is working on mobile emulator, but the problem is with device (Lumia 950)

    On the PC and mobile emulator I get the response 200 (OK)

    However on the mobile device I get 301 Moved Permanently

    I cant seem to figure out why this is happening.

    Thursday, July 7, 2016 9:22 AM

Answers

  • Well, problem solved! :) 

    It has nothing with Windows Mobile etc... The problem was, that I have to first time, when connecting to the device open just his IP address f.e. via webView etc.. then my IP is temporally stored in the device and is willing to communicate with me.. From PC it was working, because I was debugging the requests ... 

    I found it, when I run logcat on the device and was checking the logs.. 
    • Proposed as answer by Sunteen Wu Tuesday, July 19, 2016 8:18 AM
    • Marked as answer by Barry Wang Thursday, July 21, 2016 9:09 AM
    Friday, July 8, 2016 4:53 PM

All replies

  • Hi, 

    In UWP apps it will be preferable if use HttpClient from Windows.Web.Http namespace instead of HttpClient from System.Net.Http namespace 

    and your code will look like

    using Windows.Web.Http;

    Uri uri = new Uri("......."); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, uri); HttpClient httpClient = new HttpClient(); HttpResponseMessage response = await httpClient.SendRequestAsync(request);




    Thursday, July 7, 2016 10:46 AM
  • I have made some investigation and it seems, that there is problem with this:

    I tried to make an URI.

    f.e. I have:

    //http://10.0.0.68/SET STB MEDIA CTRL {"type":"tv","action":"start query status"}

    var url = "http://" + "10.0.0.68" + "/SET STB MEDIA CTRL {\"type\":\"tv\",\"action\":\"start query status\"}";

    var newUri = new Uri(url);

    Absolute path of this URI is: 

    "http://10.0.0.68/SET%20STB%20MEDIA%20CTRL%20%7B%22type%22:%22tv%22,%22action%22:%22start%20query%20status%22%7D"

    I installed fiddler and when I enter to the browser this url: 

    "http://10.0.0.68/SET STB MEDIA CTRL {"type":"tv","action":"start query status"}"

    I can see, that it is converted to this url: 

    "http://10.0.0.68/SET%20STB%20MEDIA%20CTRL%20%7B%22type%22%3A%22tv%22%2C%22action%22%3A%22start%20query%20status%22%7D"

    So its are two differents URLs.. And the problem seems to be in ":" 

    Thursday, July 7, 2016 12:18 PM
  • Hello, thanks for answer, but you solution also does not work... 

    it seems, that there is problem with ":" in url string.. 

    Peter

    Thursday, July 7, 2016 12:52 PM
  •  this part of  {"type":"tv","action":"start query status"}" your URI look like JSON  name value pairs. From where you get this URL ? 


    Thursday, July 7, 2016 2:17 PM
  • It is from DVB-S2 reciever, I am working on communication software with this reciever, there is an interface and you can remote control it via the HTTP.. 

    There is several types of command, for example: 

    http://10.0.0.68/GET GROUP LIST 1 = will return to you json with channels at group 1
    http://10.0.0.68/SET CHANNEL 197 = will change the tv channel to the 197

    this both is already working, I have done also streaming from the reciever, but following commands are not working on windows mobile

    (on PC and mobile emulator working)

    http://10.0.0.68/GET NOWORNEXT EPG { "count" : "30", "group" : 6, "epgNowOrNextFlag" : "1", "startIdx" : "0" }

    Here I say, give me first 30 channels from Group 6, starting at 0 index. - Also I should get list of channels, but without success

    same with this call: 

    http://10.0.0.68/SET STB MEDIA CTRL {"type":"tv","action":"start query status"} 

    - It means, that I would like to receive video stream TV to device or remote control it and that he should disconct other clients (if there are some)

    Thursday, July 7, 2016 3:48 PM
  • try this one

    using System.Net.Http;
    
       private async void SendRequest()
            {
                
                List<KeyValuePair<string, string>> d = new List<KeyValuePair<string, string>>();
                d.Add(new KeyValuePair<string, string>("type","tv"));
                d.Add(new KeyValuePair<string, string>("action", "start query status"));
               
                HttpClient client = new HttpClient();
                HttpContent content = new FormUrlEncodedContent(d);
                 var t = await client.PostAsync("http://10.0.0.68/SETSTBMEDIACTRL", content);
    
    
    
            }

    Thursday, July 7, 2016 5:08 PM

  • Also did not helped.. well I thinked about it and I found, that I do not need to use HttpClient - I created socket client - according microsoft tutorial

    I simulate web browser via this client, but sending just short request: 

     var stringToSend =
                    "GET /GET%20NOWORNEXT%20EPG%20%7B%20%22count%22%20%3A%20%2230%22%2C%20%22group%22%20%3A%20%225%22%2C%20%22epgNowOrNextFlag%22%20%3A%20%221%22%2C%20%22startIdx%22%20%3A%20%220%22%7D HTTP/1.1"

    When I use I debug from PC the reply is correct: 

    "HTTP/1.1 200 OK\r\nAccess-Control-Allow-Origin: *\r\nDate: Fri, 08 Jul 2016 14:20:31 GMT\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n200 {\"group\":5,\"count\":30,\"data\": etc.... "

    but from Windows Mobile phone the result is: 

    "HTTP/1.1 200 OK\r\nAccess-Control-Allow-Origin: *\r\nDate: Fri, 08 Jul 2016 15:42:01 GMT\r\nContent-Type: text/html\r\nConnection: Close\r\n\r\n301"

    So again the reply is 301... 

    Impossible.... 


    Friday, July 8, 2016 3:51 PM
  • Well, problem solved! :) 

    It has nothing with Windows Mobile etc... The problem was, that I have to first time, when connecting to the device open just his IP address f.e. via webView etc.. then my IP is temporally stored in the device and is willing to communicate with me.. From PC it was working, because I was debugging the requests ... 

    I found it, when I run logcat on the device and was checking the logs.. 
    • Proposed as answer by Sunteen Wu Tuesday, July 19, 2016 8:18 AM
    • Marked as answer by Barry Wang Thursday, July 21, 2016 9:09 AM
    Friday, July 8, 2016 4:53 PM
  • Hi Petritim,

    Thanks for the sharing. Please mark your answer to make it easier for other visitors to find the resolution later.

    Thanks for understanding.


    Best Regards, Sunteen Wu

    Tuesday, July 19, 2016 8:20 AM