我创建了支持Android和iOS移动平台的Xamrin原生共享项目。我想在两个移动应用程序中使用REST服务。如果我使用HttpClient向REST API发出请求,那么它就不起作用。给我回复:
{StatusCode:404,ReasonPhrase:'Not Found',版本:1.1,内容: System.Net.Http.StreamContent,Headers:{Vary:Accept-Encoding 服务器:DPS / 1.0.3 X-SiteId:1000 Set-Cookie:dps_site_id = 1000;路径= / 日期:2016年7月27日星期三12:09:00 GMT连接:保持活力 内容类型:text / html; charset = utf-8内容长度:964}} 内容:{System.Net.Http.StreamContent}标题:{Vary: Accept-Encoding Server:DPS / 1.0.3 X-SiteId:1000 Set-Cookie: dps_site_id = 1000; path = / Date:Wed,27 Jul 2016 12:09:00 GMT 连接:keep-alive} IsSuccessStatusCode:false ReasonPhrase: “未找到”StatusCode:System.Net.HttpStatusCode.NotFound版本: {1.1}非公开成员:
如果我使用HttpWebResponse发出请求,它会成功获取数据。
你能说出为什么HttpClient无法正常工作吗?
// Using HttpClient
public async Task<string> GetCategories11(string token)
{
using (HttpClient client = new HttpClient())
{
var url = string.Format("{0}{1}", BaseUrl, CategoriesEndPoint);
var uri = new Uri(url);
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
try
{
using (var response = await client.GetAsync(uri))
{
if (response.IsSuccessStatusCode)
{
var contentStr = await response.Content.ReadAsStringAsync();
return contentStr;
}
else
return null;
}
}
catch
{
return null;
}
}
}
// Using HttpWebRequest
public async Task<ResponseModel> GetCategories(string token)
{
// Create an HTTP web request using the URL:
var url = string.Format("{0}{1}", RequestClient.BaseUrl, CategoriesEndPoint);
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(url));
request.ContentType = "application/json";
request.Headers.Add("Authorization", "Bearer " + token);
request.Accept = "application/json";
request.Method = "GET";
try
{
// Send the request to the server and wait for the response:
using (HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync())
{
// Get a stream representation of the HTTP web response.
using (Stream stream = response.GetResponseStream())
{
// Use this stream to build a JSON object.
JsonValue jsonDoc = await Task.Run(() => JsonObject.Load(stream));
return new ResponseModel() { Success = true, ResponseValue = jsonDoc.ToString(), StatusCode = response.StatusCode };
}
}
}
catch (WebException ex)
{
using (var stream = ex.Response.GetResponseStream())
using (var reader = new StreamReader(stream))
{
return new ResponseModel() { ResponseValue = reader.ReadToEnd(), StatusCode = ((HttpWebResponse)ex.Response).StatusCode };
}
}
catch (Exception ex)
{
return new ResponseModel() { ResponseValue = ex.Message };
}
}
答案 0 :(得分:0)
通过func moviePlayerDidEnterFullscreen (notification : NSNotification) {
self.window?.addGestureRecognizer(swipeUpGestureRecognizer)
}
调试并暂停using (var response = await client.GetAsync(uri))
是什么?它与GetCategories()中的相同?
如果您愿意,这是我在Xamarin.Android中使用的方法,它可以使用持票人令牌。根据您的需要进行更改,您可能不需要执行uri
部分。
JsonConvert.DeserializeObject()