InvalidCastException HttpWebRequest c#
我有一个问题:当我在 BackgroundAgent 中创建 HttpWebRequest 时,应用程序抛出 InvalidCastException。 此代码适用于应用程序前台任务,但不适用于BackgroundAgent:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(//InvalidCastException
new Uri(url));
request.BeginGetResponse(r => {
HttpWebRequest httprequest = (HttpWebRequest)r.AsyncState;
try {
I have a problem: app throws InvalidCastException when I creating HttpWebRequest in BackgroundAgent.
This code works in App foreground tasks, but doesn't works in BackgroundAgent:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(//InvalidCastException
new Uri(url));
request.BeginGetResponse(r => {
HttpWebRequest httprequest = (HttpWebRequest)r.AsyncState;
try {
Full code: http://pastebin.com/zyCHBQuP
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
返回的类型取决于传递到 Create 方法中的 Uri。您将得到 WebRequest 的一些后代。如果要进行该转换,您必须确保传递的 Uri 属于将返回 HttpWebRequest 的类型,否则您需要在转换之前测试从 Create 返回的类型或使用
as HttpWebRequest.
http://msdn.microsoft.com/en-us/library/0aa3d588.aspx (对于 .net)
http://msdn.microsoft.com/en-us /library/0aa3d588%28v=VS.95%29.aspx(对于 silverlight)
The type returned is dependent on the Uri passed into the Create method. You will get some descendant of WebRequest. You must be sure the Uri you pass is of the type that will return an HttpWebRequest if you are going to make that cast, or you will need to test the type returned from Create prior to casting or use the
as HttpWebRequest
.http://msdn.microsoft.com/en-us/library/0aa3d588.aspx (for .net)
http://msdn.microsoft.com/en-us/library/0aa3d588%28v=VS.95%29.aspx (for silverlight)