C#,WebRequest.Create(String URI)返回null

时间:2011-11-10 20:58:58

标签: c# .net silverlight webrequest

我正在尝试创建一个WebRequest来获取数据。但是,由于某些原因,我无法创建请求,因为WebRequest.Create()返回一个空对象。

我的代码如下:

WebRequest.RegisterPrefix("http://", WebRequestCreator.BrowserHttp);
WebRequest test = WebRequest.Create("http://www.google.com");

当我单步执行时,在第二行之后,test始终为null - 不会抛出任何异常。

我不知道为什么返回null对象。这种行为的可能原因是什么?

3 个答案:

答案 0 :(得分:1)

我的感觉是第一行不是必需的。试试

WebRequest test = WebRequest.Create("http://www.google.com");

答案 1 :(得分:0)

 you can use following instead...

WebClient client = new WebClient();
client.Encoding = System.Text.Encoding.UTF8;// GetEncoding(1252);
UrlContent =client.DownloadString(url);

答案 2 :(得分:0)

在使用Silverlight(或开发Moonlight ;-)时,我不记得这样的问题。也许已经注册了其他东西?

无论如何你应该可以使用:

WebRequestCreator.BrowserHttp.Create (new Uri ("http://www.google.com"));

获得相同的结果,即确保使用浏览器 HTTP堆栈创建WebRequest实例。

相关问题