Silverlight 4 中的 HttpWebRequest
我正在尝试创建 httpwebrequest
到一个 url (REST API),我正在其中将流写入目标 api 服务器。但在写入流之前,在我的请求对象中:用户代理抛出错误,'request.UserAgent'
抛出'System.NotImplementedException'
类型的异常。即使我也有硬编码的 useragent
值。其他两个参数 AllowAutoRedirect
和 CookieContainer
的情况相同。另一方面,所有其他参数都具有正确的值或为空。
对此有何帮助,为什么 UserAgent
参数抛出此错误 'request.UserAgent'
抛出 'System.NotImplementedException'
类型的异常。以下是我的网络请求:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri("TargetAPIIUrl_I am passing here"));
request.Method = "POST";
string boundary = "---------------" + DateTime.Now.Ticks.ToString();
string formDataBoundary = "-----------------------------" + DateTime.Now.Ticks.ToString().Substring(0, 14);
string contentType = "multipart/form-data; boundary=" + formDataBoundary;
request.ContentType = contentType;
request.UserAgent = "Hardcoded string of my target API";
request.BeginGetRequestStream(new AsyncCallback(asyncResult =>
{
Stream stream = request.EndGetRequestStream(asyncResult);
SilverlightApplication1.TubeUtility.DataContractMultiPartSerializer ser = new SilverlightApplication1.TubeUtility.DataContractMultiPartSerializer(boundary);
ser.WriteObject(stream, parameters);
stream.Close();
request.BeginGetResponse(callback, request);
}), request);
I am trying to create httpwebrequest
to one url (REST API) where i am writing stream to target api server. But before writing stream, in my request object : User Agent is throwing error that 'request.UserAgent'
threw an exception of type 'System.NotImplementedException'
. Even i have hard coded useragent
value also. Same case with other two params AllowAutoRedirect
and CookieContainer
. On the other hand all other params having correct value or null.
Any help on this, why UserAgent
param is throwing this error 'request.UserAgent'
threw an exception of type 'System.NotImplementedException'
. Below is my web request:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri("TargetAPIIUrl_I am passing here"));
request.Method = "POST";
string boundary = "---------------" + DateTime.Now.Ticks.ToString();
string formDataBoundary = "-----------------------------" + DateTime.Now.Ticks.ToString().Substring(0, 14);
string contentType = "multipart/form-data; boundary=" + formDataBoundary;
request.ContentType = contentType;
request.UserAgent = "Hardcoded string of my target API";
request.BeginGetRequestStream(new AsyncCallback(asyncResult =>
{
Stream stream = request.EndGetRequestStream(asyncResult);
SilverlightApplication1.TubeUtility.DataContractMultiPartSerializer ser = new SilverlightApplication1.TubeUtility.DataContractMultiPartSerializer(boundary);
ser.WriteObject(stream, parameters);
stream.Close();
request.BeginGetResponse(callback, request);
}), request);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
UserAgent
和AllowAutoRedirect
属性的存在是为了与 .NET 框架HttpWebRequest
保持一定的一致性,但是 ClientHTTP 和 BrowserHTTP 实现都不支持它们。可以将
CookieContainer
与 ClientHTTP 堆栈一起使用,BrowserHTTP 堆栈当然将使用主机浏览器 cookie 管理。The
UserAgent
andAllowAutoRedirect
properties are present to maintain some consistency with the .NET frameworkHttpWebRequest
however neither the ClientHTTP nor the BrowserHTTP implementations support them.It is possible to use a
CookieContainer
with the ClientHTTP stack, the BrowserHTTP stack will of course use the host browser cookie management.