WebRequest System.NotSupportedException

时间:2015-05-13 01:29:16

标签: c# .net mono monodevelop webrequest

在monodevelop中写道:

using System;
using System.Net;
using System.Net.Http;

namespace Hello
{
    public class Hello
    {
        public Hello () {}

        public static WebResponse world(string symbol) {
            WebRequest request = WebRequest.Create("http://yahoo.com");
            WebResponse response = request.GetResponse ();
        }
    }
}

在运行时获取此异常:

  

未处理的例外情况:   System.NotSupportedException:http://yahoo.com/     在System.Net.WebRequest.GetCreator(System.String前缀)[0x00000]中:0     在System.Net.WebRequest.Create(System.Uri requestUri)[0x00000] in:0     在System.Net.WebRequest.Create(System.String requestUriString)[0x00000] in:0     at CNBC.Research.LookupUtil.downloadStockHistory(System.String symbol)[0x00000] in:0     在CNBC.MainClass.Main(System.String [] args)[0x00000] in:0   [错误]致命的不受欢迎的异常:System.NotSupportedException:http://yahoo.com/     在System.Net.WebRequest.GetCreator(System.String前缀)[0x00000]中:0     在System.Net.WebRequest.Create(System.Uri requestUri)[0x00000] in:0     在System.Net.WebRequest.Create(System.String requestUriString)[0x00000] in:0     在CNBC.Research.Hello.world(System.String symbol)[0x00000] in:0     at CNBC.MainClass.Main(System.String [] args)[0x00000] in:0

我相信这是因为它不知道http是什么,但我不知道如何添加它。

编辑:

这可能会解决这个问题。可能是因为我运行的是错误的版本?我也收到了这个错误:

  

警告:此应用程序支持的运行时版本不可用。   使用默认运行时:v4.0.30319

我应该为.NET 3.5运行3.5版本吗?

2 个答案:

答案 0 :(得分:0)

请尝试以下代码段。

   public static WebResponse world(string symbol) {
        // Create a new 'Uri' object with the specified string.
        Uri myUri =new Uri("http://yahoo.com");
        // Create a new request to the above mentioned URL. 
        WebRequest request= WebRequest.Create(myUri);
        // Assign the response object of 'WebRequest' to a 'WebResponse' variable.
        WebResponse response = request.GetResponse ();

      }

OR

 public static WebResponse world(string symbol) {
        // Create a new 'Uri' object with the specified string.
        Uri myUri =new Uri("http://yahoo.com");
        // Create a new request to the above mentioned URL. 
        WebRequest request= CreateWebRequest(myUri);
        // Assign the response object of 'WebRequest' to a 'WebResponse' variable.
        WebResponse response = request.GetResponse ();

      }


private static HttpWebRequest CreateWebRequest(Uri uri)
{
 var type = Type.GetType("System.Net.HttpRequestCreator, System, Version=4.0.0.0,Culture=neutral, PublicKeyToken=b77a5c561934e089");
    var creator = Activator.CreateInstance(type,nonPublic:true) as IWebRequestCreate;
    return creator.Create(uri) as HttpWebRequest;
}

答案 1 :(得分:-1)

我尝试将我的应用程序与mono运行时捆绑在一起,以便在没有安装运行时的机器上使用时,我遇到了这种情况。

解决方案是将 App.config 文件添加到我的项目中,其中包含以下内容(从已安装的单声道版本的machine.config文件中复制的部分)

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <sectionGroup name="system.net" type="System.Net.Configuration.NetSectionG

    roup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
            <section name="authenticationModules" type="System.Net.Configuration.AuthenticationModulesSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
            <section name="connectionManagement" type="System.Net.Configuration.ConnectionManagementSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
            <section name="defaultProxy" type="System.Net.Configuration.DefaultProxySection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
            <sectionGroup name="mailSettings" type="System.Net.Configuration.MailSettingsSectionGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
                <section name="smtp" type="System.Net.Configuration.SmtpSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
            </sectionGroup>
            <section name="requestCaching" type="System.Net.Configuration.RequestCachingSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
            <section name="settings" type="System.Net.Configuration.SettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
            <section name="webRequestModules" type="System.Net.Configuration.WebRequestModulesSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
        </sectionGroup>
      </configSections>

      <system.net>
        <authenticationModules>
            <add type="System.Net.BasicClient, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
            <add type="System.Net.DigestClient, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
            <add type="System.Net.NtlmClient, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
        </authenticationModules>
        <webRequestModules>
            <add prefix="http" type="System.Net.HttpRequestCreator, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
            <add prefix="https" type="System.Net.HttpRequestCreator, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
            <add prefix="file" type="System.Net.FileWebRequestCreator, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
            <add prefix="ftp" type="System.Net.FtpRequestCreator, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
        </webRequestModules>
        <settings>
          <ipv6 enabled="false"/>
          <servicePointManager checkCertificateName="false" checkCertificateRevocationList="false" />
        </settings>
      </system.net>


    </configuration>

希望这会给你一个线索!