Languages

Menu
Sites
Language
HttpWebRequest and WebProxy to get json from url - tizen 4 galaxy watch

Hi! I'm try to get json from url (stop_url) without success. When it tries to set webproxy (line 14), it gives me an exception(line 48) "Invalid URI: The hostname could not be parsed.". Code below:

 

if (!stop_url_.Equals(""))
                {
                    string html = null;
                    try
                    {
                        ConnectionItem currentConnection = ConnectionManager.CurrentConnection;
                        Console.WriteLine("connection(" + currentConnection.Type + ", " + currentConnection.State);
                        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(stop_url_);
                        //request.ContentType = "application/json";

                        // When a watch is paired with a mobile device, we can use WebProxy.
                        if (currentConnection.Type == ConnectionType.Ethernet)
                        {
                            string proxyAddr = ConnectionManager.GetProxy(AddressFamily.IPv4);
                            WebProxy myproxy = new WebProxy(proxyAddr, true);
                            request.Proxy = myproxy;

                            request.Method = "GET";
                            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                            Console.WriteLine("StatusDescription: " + ((HttpWebResponse)response).StatusDescription);

                            // Get the stream containing content returned by the server.
                            Stream dataStream = response.GetResponseStream();
                            // Open the stream using a StreamReader for easy access.
                            StreamReader reader = new StreamReader(dataStream);
                            // Read the content.  
                            string responseFromServer = reader.ReadToEnd();
                            // Display the content.  
                            Console.WriteLine("responseFromServer :" + responseFromServer);
                            // Clean up the streams and the response.
                            reader.Close();
                            response.Close();
                            html = responseFromServer;
                            Console.WriteLine(html);
                        }
                        else
                        {
                            WebClient wc = new WebClient();
                            html = wc.DownloadString(stop_url_);
                            Console.WriteLine(html);
                        }

                        //Se ho scaricato correttamente l'html
                        if (html != null && !html.Equals("")) doc = NSoupClient.Parse(html);
                        else time_error = -1; //connection error
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("An error occurs : " + ex.Message);
                    }
                }

Any suggest?

View Selected Answer

Responses

3 Replies
Mark as answer
Tizen .NET

Hi

WebProxy setting is required only if Galaxy Watch and mobile phone are paried via BT. Is this the case?

WebProxy myproxy = new WebProxy(proxyAddr, true);

Could you check the format of proxyAddr and value?

https://www.codeproject.com/Questions/1064600/Error-Invalid-URI-The-hostname-could-not-be-parsed

https://docs.microsoft.com/en-us/dotnet/framework/network-programming/how-to-enable-a-webrequest-to-use-a-proxy-to-communicate-with-the-internet

In my case, proxyAddr is 192.168.153.100:8080.

 

 

 

Tizen .NET

Plus, please share binary information. You can check Settings app on your Galaxy Watch.

Settings > About watch > Software > Software version

feduss
Hi! Thanks for your reply! I didn't specify that i was trying with the emulator. With galaxy watch i don't have any problem. So, problem fixed :)