Problem fetching page in Windows Phone 8.1, VB.NET
-
I'm trying to make a small app to fetch some numbers from my smarthome webpage. When trying the query in a browser like Chrome (uri: http://smarthome.trab.dk/wpf/getData.php?g=power) everything works. In the app, it just stalls - no exceptions or errors but no data either. The module is like this:
Public uriString As String = "http://smarthome.trab.dk" Public Async Function getData(ByVal gType As String) As Tasks.Task(Of String) Try Dim Resp As String = Nothing Using httpClient As New HttpClient httpClient.BaseAddress = New Uri(uriString) httpClient.Timeout = TimeSpan.FromSeconds(10) Dim response As HttpResponseMessage = Await httpClient.GetAsync("/wpf/getData.php?g=" + gType.ToLower) Resp = Await response.Content.ReadAsStringAsync End Using Return Resp Catch webEx As HttpRequestException MessageBox.Show(webEx.Message) Catch ex As Exception MessageBox.Show(ex.Message) End Try End Function Public Function ACount() As String Dim resp As String = getData("power").Result Return "Actual: " + resp End Function
Can anyone see what the problem is?
Regards, Morten Trab
-
I'm trying to make a small app to fetch some numbers from my smarthome webpage. When trying the query in a browser like Chrome (uri: http://smarthome.trab.dk/wpf/getData.php?g=power) everything works. In the app, it just stalls - no exceptions or errors but no data either. The module is like this:
Public uriString As String = "http://smarthome.trab.dk" Public Async Function getData(ByVal gType As String) As Tasks.Task(Of String) Try Dim Resp As String = Nothing Using httpClient As New HttpClient httpClient.BaseAddress = New Uri(uriString) httpClient.Timeout = TimeSpan.FromSeconds(10) Dim response As HttpResponseMessage = Await httpClient.GetAsync("/wpf/getData.php?g=" + gType.ToLower) Resp = Await response.Content.ReadAsStringAsync End Using Return Resp Catch webEx As HttpRequestException MessageBox.Show(webEx.Message) Catch ex As Exception MessageBox.Show(ex.Message) End Try End Function Public Function ACount() As String Dim resp As String = getData("power").Result Return "Actual: " + resp End Function
Can anyone see what the problem is?
Regards, Morten Trab
I don't use VB. But you are expecting a numeric value and are returning a string (I think).:confused:
David
-
I don't use VB. But you are expecting a numeric value and are returning a string (I think).:confused:
David
The line it strands on are:
Dim response As HttpResponseMessage = Await httpClient.GetAsync("/wpf/getData.php?g=" + gType.ToLower)
Way before returning anything to the app.
-
The line it strands on are:
Dim response As HttpResponseMessage = Await httpClient.GetAsync("/wpf/getData.php?g=" + gType.ToLower)
Way before returning anything to the app.
I tried to work with your code but I was a bit lost(I don't do vb). But I threw this together in no time and retrieved the result You wanted.
static void Main(string[] args)
{
Uri addr = new Uri( "http://smarthome.trab.dk/wpf/getData.php?g=power");
using (WebClient client = new WebClient())
{
string responce = client.DownloadString (addr);
Console.WriteLine(responce);
Console.ReadLine();
}
}I hope this helps.
David
-
I tried to work with your code but I was a bit lost(I don't do vb). But I threw this together in no time and retrieved the result You wanted.
static void Main(string[] args)
{
Uri addr = new Uri( "http://smarthome.trab.dk/wpf/getData.php?g=power");
using (WebClient client = new WebClient())
{
string responce = client.DownloadString (addr);
Console.WriteLine(responce);
Console.ReadLine();
}
}I hope this helps.
David
Did you test this on a mobile device? Errors: 'Using' operand of type 'System.Net.WebClient' must implement 'System.IDisposal' 'DownloadString' is not a member of 'System.Net.WebClient'
-
I tried to work with your code but I was a bit lost(I don't do vb). But I threw this together in no time and retrieved the result You wanted.
static void Main(string[] args)
{
Uri addr = new Uri( "http://smarthome.trab.dk/wpf/getData.php?g=power");
using (WebClient client = new WebClient())
{
string responce = client.DownloadString (addr);
Console.WriteLine(responce);
Console.ReadLine();
}
}I hope this helps.
David
Your example lead me to this, found on another site:
Public Sub getData(ByVal gType As String)
Dim addr As Uri = New Uri("http://smarthome.trab.dk/wpf/getData.php?g=" + gType)Dim client As New WebClient() AddHandler client.DownloadStringCompleted, AddressOf client\_DownloadStringCompleted client.DownloadStringAsync(addr)
End Sub
Private Sub client_DownloadStringCompleted(sender As Object, e As DownloadStringCompletedEventArgs)
Dim data As String = e.Result MessageBox.Show(data)
End Sub
And it returns the data :)
-
Your example lead me to this, found on another site:
Public Sub getData(ByVal gType As String)
Dim addr As Uri = New Uri("http://smarthome.trab.dk/wpf/getData.php?g=" + gType)Dim client As New WebClient() AddHandler client.DownloadStringCompleted, AddressOf client\_DownloadStringCompleted client.DownloadStringAsync(addr)
End Sub
Private Sub client_DownloadStringCompleted(sender As Object, e As DownloadStringCompletedEventArgs)
Dim data As String = e.Result MessageBox.Show(data)
End Sub
And it returns the data :)
Glad You found an answer. Good luck!
David