简体   繁体   English

WP7 App的HttpWebRequest抛出NullReferenceException

[英]WP7 App's HttpWebRequest throws NullReferenceException

I have the following code to create an HttpWebRequest in my WP7 application. 我有以下代码在我的WP7应用程序中创建HttpWebRequest。 I'm trying to call this code from a Unit Test application which is of type "Silverlight Unit Test Application" with the target framework set to Silverlight 5. I grabbed the WP7.5 Mango Beta test assemblies from Jeff Wilcox's blog: 我试图从单元测试应用程序调用此代码,该应用程序是“Silverlight单元测试应用程序”类型,目标框架设置为Silverlight 5.我从Jeff Wilcox的博客中抓取了WP7.5 Mango Beta测试程序集:

public void SomeFunction()
{
    Uri uri = new Uri("http://www.google.com");
    HttpWebRequest request = HttpWebRequest.CreateHttp(uri);
    request.Method = "POST";
    ...
}

In the code above, the line that creates the request throws a NullReferenceException . 在上面的代码中,创建请求的行会抛出NullReferenceException The stack trace doesn't provide anything of value. 堆栈跟踪不提供任何有价值的东西。

Is this happening because I'm using an SL Test Application? 这是因为我正在使用SL测试应用程序吗?

Windows Phone 7.5 uses Silveright 4, not 5, but it's not compatible with the desktop versions of either (unit tests must be run from the WP emulator). Windows Phone 7.5使用Silveright 4而不是5,但它与桌面版本不兼容(必须从WP模拟器运行单元测试)。

To create a WP test project: 要创建WP测试项目:

  1. Create a Windows Phone Silverlight Application project 创建Windows Phone Silverlight应用程序项目
  2. Add a reference to your actual application project 添加对实际应用程序项目的引用
  3. Download and reference the SL Unit Testing assemblies for Mango 下载并参考芒果SL单元测试组件
  4. Add the following code to MainPage in the Loaded event handler: 将以下代码添加到Loaded事件处理程序中的MainPage:

_ _

var testPage = UnitTestSystem.CreateTestPage() as IMobileTestPage; 
BackKeyPress += (x, xe) => xe.Cancel = testPage.NavigateBack(); 
(Application.Current.RootVisual as PhoneApplicationFrame).Content = testPage; 

On topic, you might be able to resolve the issue by re-registering the correct handlers: 关于主题,您可以通过重新注册正确的处理程序来解决此问题:

WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp);
WebRequest.RegisterPrefix("https://", WebRequestCreator.ClientHttp);

However, I'd recommend you just start again. 但是,我建议你重新开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM