I'm developing a Windows 8.1 Store app with .NET Framework 4.5.1.
I'm trying to connect to my own ASP.NET Web Api 2.2 Restful service. I have test it with PostMan an it works perfectly.
I'm doing a GET with the following code:
using Windows.Web.Http;
public async Task<string> GET(string url)
{
Uri resourceUri;
if (!Uri.TryCreate(url.Trim(), UriKind.Absolute, out resourceUri))
{
//statusText.Text = "Invalid URI, please re-enter a valid URI";
return null;
}
if (resourceUri.Scheme != "http" && resourceUri.Scheme != "https")
{
//statusText.Text = "Only 'http' and 'https' schemes supported. Please re-enter URI";
return null;
}
_customError.Reset();
HttpClient request = new HttpClient();
HttpResponseMessage response = new HttpResponseMessage();
var headers = request.DefaultRequestHeaders;
// Try to add user agent to headers.
if (headers.UserAgent.TryParseAdd(_userAgent))
headers.UserAgent.ParseAdd(_userAgent);
try
{
//response = await request.GetAsync(resourceUri);
//response.EnsureSuccessStatusCode();
//return await response.Content.ReadAsStringAsync();
return await request.GetStringAsync(resourceUri);
}
catch (WebException ex)
{
WebResponse errorResponse = ex.Response;
if (errorResponse != null)
{
using (Stream responseStream = errorResponse.GetResponseStream())
{
StreamReader reader =
new StreamReader(responseStream, Encoding.GetEncoding("utf-8"));
String errorText = reader.ReadToEnd();
_customError = JsonConvert.DeserializeObject<CustomHttpErrorMessage>(errorText);
}
}
throw;
}
}
I have debugged both, client and Web Api, and Windows client reach Web Api, and Web Api controller returns NotFound(), but the executions doesn't continue on Windows client.
This is the first time I develop a Windows 8.1 and the first time I use HttpClient and I don't know why I don't get any response from Web Api.
Do you what's going on?
No comments:
Post a Comment