req.Headers.Add("APP_TYPE", "IOSAPP");
我正在开发一个Xamarin表单应用程序,并希望从我的webapi获取数据。当我输入request.Headers.Add
时,我在添加此消息时出现了一条波浪线
“/ Users / etgmacmini3 / Projects / sample1 / sample1 / GlobalClass.cs(16,16):错误CS1061:'WebHeaderCollection'不包含'Add'的定义,也没有扩展方法'Add'接受第一个参数可以找到'WebHeaderCollection'类型(您是否缺少using指令或汇编引用?)(CS1061)(sample1)“
答案 0 :(得分:0)
不确定“req”对象的类型是什么,但是如果你遵循这个,它应该让你开始向Web API发送HTTP请求:
using System.Net;
using System.Net.Http;
var httpClient = new HttpClient();
var data = "Some JSON data to be sent in your request body"; //May not need this, based on your app needs
var contentType = "application/json"; //May vary based on your app
var httpMethod = HttpMethod.Post; //or Get, or whatever HTTP verb your API endpoint needs
var request = new HttpRequestMessage()
{
RequestUri = new Uri("http://YourWebApiUrlHere"),
Method = httpMethod,
Content = new StringContent(data, System.Text.Encoding.UTF8, contentType)
};
request.Headers.Add("YourHeaderName", "YourHeaderValue");
var httpResponse = await httpClient.SendAsync(request);
if (httpResponse.StatusCode == HttpStatusCode.OK)
{
//It worked, so do something
}
else
{
//It didn't work, so do something else
}