C# WebHeaderCollection&;Xamarin上的HttpWebRequest
我正在尝试将我的项目从Mac上的Xamarin Studio移植到Windows 7上的Visual Studio 2012。 在Mac和XS上都可以正常工作。在VisualStudio 2012上,我遇到了以下两个问题: 错误3“System.Net.WebHeaderCollection”不包含定义 对于'Add'和无扩展方法'Add',接受的第一个参数为 未能找到“System.Net.WebHeaderCollection”类型(是否缺少 使用指令或程序集 参考?)C:\Users\user\Documents\visualstudio 2012\Projects\MyProject\MyProject.Core\Services\MyProjectService.cs 错误4“System.Net.HttpWebRequest”不包含的定义 “GetResponse”和没有扩展方法“GetResponse”接受第一个 找不到类型为“System.Net.HttpWebRequest”的参数(是否为 缺少using指令或程序集 参考?)C:\Users\user\Documents\visualstudio 2012\Projects\MyProject\MyProject.Core\Services\MyProjectService.cs 关于该守则:C# WebHeaderCollection&;Xamarin上的HttpWebRequest,c#,mono,httpwebrequest,xamarin,xamarin-studio,C#,Mono,Httpwebrequest,Xamarin,Xamarin Studio,我正在尝试将我的项目从Mac上的Xamarin Studio移植到Windows 7上的Visual Studio 2012。 在Mac和XS上都可以正常工作。在VisualStudio 2012上,我遇到了以下两个问题: 错误3“System.Net.WebHeaderCollection”不包含定义 对于'Add'和无扩展方法'Add',接受的第一个参数为 未能找到“System.Net.WebHeaderCollection”类型(是否缺少 使用指令或程序集 参考?)C:\Users\us
var request = WebRequest.Create("https://www.myaddress.com/test/") as HttpWebRequest;
request.Method = "GET";
request.Accept = "application/json";
request.Headers.Add(HttpRequestHeader.Cookie,"mycookievalue");
// Get response
using (var response = request.GetResponse() as HttpWebResponse)
{
// Get the response stream
var reader = new StreamReader(response.GetResponseStream());
content = reader.ReadToEnd();
}
我该怎么解决呢?到目前为止,您提到的事情还没有实现。
不过没关系。他们仍然做得很好
回答你的问题
错误3。对目前,您只能使用头[“键”]=value。但是不是每个标题都有。我试图将“Content Length”放在那里(因为也没有实现“ContentLength”属性),但得到了“restricted headers”异常。
但是,请求POST为我成功处理,所以我放弃了它
错误4。对没有这样的方法。(与此相同,因为没有“GetRequestStream”(例如,如果您想将POST数据写入请求流)。好消息是,分别有BeginGetResponse和BeginGetRequestStream,还有C#动作方法简化了所有厨房
(另外,我确信,这对于理解异步实践非常有用)
基本样品可在上找到
但为了实现这一点,我使用了以下方法:
public void MakeRequest(string url, string verb, Dictionary<string, string> requestParams,
Action<string> onSuccess, Action<Exception> onError)
{
string paramsFormatted;
if (verb == "GET")
{
paramsFormatted = string.Join("&", requestParams.Select(x => x.Key + "=" + Uri.EscapeDataString(x.Value)));
url = url + (string.IsNullOrEmpty(paramsFormatted) ? "" : "?" + paramsFormatted);
}
else
{
// I don't escape parameters here,
// as Uri.EscapeDataString would throw exception if the parameter length
// is too long, so you must control that manually before passing them here
// for instance to convert to base64 safely
paramsFormatted = string.Join("&", requestParams.Select(x => x.Key + "=" + (x.Value)));
}
var request = (HttpWebRequest)WebRequest.Create(url);
request.Method = verb;
requestParams = requestParams ?? new Dictionary<string, string>();
Action goRequest = () => MakeRequest(request,
response =>
{
onSuccess(response);
},
error =>
{
if (onError != null)
{
onError(error);
}
});
if (request.Method == "POST")
{
request.BeginGetRequestStream(ar =>
{
using (Stream postStream = request.EndGetRequestStream(ar))
{
byte[] byteArray = Encoding.UTF8.GetBytes(paramsFormatted);
postStream.Write(byteArray, 0, paramsFormatted.Length);
}
goRequest();
}, request);
}
else // GET
{
goRequest();
}
}
private void MakeRequest(HttpWebRequest request, Action<string> onSuccess, Action<Exception> onError)
{
request.BeginGetResponse(token =>
{
try
{
using (var response = request.EndGetResponse(token))
{
using (var stream = response.GetResponseStream())
{
var reader = new StreamReader(stream);
onSuccess(reader.ReadToEnd());
}
}
}
catch (WebException ex)
{
onError(ex);
}
}, null);
}
public void MakeRequest(字符串url、字符串动词、字典请求参数、,
成功时的操作,错误时的操作)
{
字符串参数已格式化;
if(动词==“GET”)
{
paramsFormatted=string.Join(“&”,requestParams.Select(x=>x.Key+“=”+Uri.EscapeDataString(x.Value));
url=url+(string.IsNullOrEmpty(paramsformated)?“”:“?”+paramsformated);
}
其他的
{
//我不会在这里逃避参数,
//如果参数长度
//太长,因此在将其传递到此处之前必须手动控制
//例如,安全地转换为base64
paramsFormatted=string.Join(“&”,requestParams.Select(x=>x.Key++“=”+(x.Value));
}
var request=(HttpWebRequest)WebRequest.Create(url);
方法=动词;
requestParams=requestParams??新建字典();
操作goRequest=()=>MakeRequest(请求,
响应=>
{
成功(响应);
},
错误=>
{
如果(onError!=null)
{
误差;
}
});
if(request.Method==“POST”)
{
request.BeginGetRequestStream(ar=>
{
使用(Stream postStream=request.EndGetRequestStream(ar))
{
byte[]byteArray=Encoding.UTF8.GetBytes(参数格式);
Write(byteArray,0,paramsformated.Length);
}
goRequest();
},请求);
}
否则//
{
goRequest();
}
}
私有void MakeRequest(HttpWebRequest请求、操作成功、操作错误)
{
request.BeginGetResponse(令牌=>
{
尝试
{
使用(var response=request.EndGetResponse(令牌))
{
使用(var stream=response.GetResponseStream())
{
变量读取器=新的流读取器(流);
onSuccess(reader.ReadToEnd());
}
}
}
捕获(WebException ex)
{
onError(ex);
}
},空);
}
到目前为止,您提到的事情还没有实现。
不过没关系。他们仍然做得很好
回答你的问题
错误3。对目前,您只能使用头[“键”]=value。但是不是每个标题都有。我试图将“Content Length”放在那里(因为也没有实现“ContentLength”属性),但得到了“restricted headers”异常。
但是,请求POST为我成功处理,所以我放弃了它
错误4。对没有这样的方法。(与此相同,因为没有“GetRequestStream”(例如,如果您想将POST数据写入请求流)。好消息是,分别有BeginGetResponse和BeginGetRequestStream,还有C#动作方法简化了所有厨房
(另外,我确信,这对于理解异步实践非常有用)
基本样品可在上找到
但为了实现这一点,我使用了以下方法:
public void MakeRequest(string url, string verb, Dictionary<string, string> requestParams,
Action<string> onSuccess, Action<Exception> onError)
{
string paramsFormatted;
if (verb == "GET")
{
paramsFormatted = string.Join("&", requestParams.Select(x => x.Key + "=" + Uri.EscapeDataString(x.Value)));
url = url + (string.IsNullOrEmpty(paramsFormatted) ? "" : "?" + paramsFormatted);
}
else
{
// I don't escape parameters here,
// as Uri.EscapeDataString would throw exception if the parameter length
// is too long, so you must control that manually before passing them here
// for instance to convert to base64 safely
paramsFormatted = string.Join("&", requestParams.Select(x => x.Key + "=" + (x.Value)));
}
var request = (HttpWebRequest)WebRequest.Create(url);
request.Method = verb;
requestParams = requestParams ?? new Dictionary<string, string>();
Action goRequest = () => MakeRequest(request,
response =>
{
onSuccess(response);
},
error =>
{
if (onError != null)
{
onError(error);
}
});
if (request.Method == "POST")
{
request.BeginGetRequestStream(ar =>
{
using (Stream postStream = request.EndGetRequestStream(ar))
{
byte[] byteArray = Encoding.UTF8.GetBytes(paramsFormatted);
postStream.Write(byteArray, 0, paramsFormatted.Length);
}
goRequest();
}, request);
}
else // GET
{
goRequest();
}
}
private void MakeRequest(HttpWebRequest request, Action<string> onSuccess, Action<Exception> onError)
{
request.BeginGetResponse(token =>
{
try
{
using (var response = request.EndGetResponse(token))
{
using (var stream = response.GetResponseStream())
{
var reader = new StreamReader(stream);
onSuccess(reader.ReadToEnd());
}
}
}
catch (WebException ex)
{
onError(ex);
}
}, null);
}
public void MakeRequest(字符串url、字符串动词、字典请求参数、,
成功时的操作,错误时的操作)
{
字符串参数已格式化;
if(动词==“GET”)
{
paramsFormatted=string.Join(“&”,requestParams.Select(x=>x.Key+“=”+Uri.EscapeDataString(x.Value));
url=url+(string.IsNullOrEmpty(paramsFormatted)?“”:“?”+paramsF