简体   繁体   English

WebRequest System.NotSupportedException

[英]WebRequest System.NotSupportedException

Wrote this in monodevelop: 在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 ();
        }
    }
}

Getting this exception where running: 在运行时获取此异常:

Unhandled Exception: System.NotSupportedException: http://yahoo.com/ at System.Net.WebRequest.GetCreator (System.String prefix) [0x00000] in :0 at System.Net.WebRequest.Create (System.Uri requestUri) [0x00000] in :0 at System.Net.WebRequest.Create (System.String requestUriString) [0x00000] in :0 at CNBC.Research.LookupUtil.downloadStockHistory (System.String symbol) [0x00000] in :0 at CNBC.MainClass.Main (System.String[] args) [0x00000] in :0 [ERROR] FATAL UNHANDLED EXCEPTION: System.NotSupportedException: http://yahoo.com/ at System.Net.WebRequest.GetCreator (System.String prefix) [0x00000] in :0 at System.Net.WebRequest.Create (System.Uri requestUri) [0x00000] in :0 at System.Net.WebRequest.Create (System.String requestUriString) [0x00000] in :0 at CNBC.Research.Hello.world (System.String symbol) [0x00000] in :0 at CNBC.MainClass.Main (System.String[] args) [0x00000] in :0 未处理的异常:System.NotSupportedException:System.Net.WebRequest.GetCreator上的http://yahoo.com/(System.String前缀)[0x00000],System.Net.WebRequest.Create上的:0(System.Uri requestUri)[在CNBC.Research.LookupUtil.downloadStockHistory(System.String符号)在0处,在CNBC.MainClass的:0处,在System.Net.WebRequest.Create(0.在00000. 0x00000]在System.Net.WebRequest.Create(System.String requestUriString)[0x00000]在[0x00000]主(System.String []参数)[0x00000]在:0 [ERROR]致命未处理的异常:System.NotSupportedException: http://yahoo.com/在System.Net.WebRequest.GetCreator(System.String前缀)[0x00000 ]在System.Net.WebRequest.Create(System.Uri requestUri)的:0中[0x00000]在System.Net.WebRequest.Create(System.String requestUriString)的:0中的CN0.CN(0x00000)在CNBC.Research.Hello .world(System.String符号)[0x00000]在CNBC.MainClass.Main的:0中[System.String [] args)[0x00000]在:0的状态

I believe it's because it doesn't know what http is but I don't know how to add it. 我相信是因为它不知道什么是http ,但是我不知道如何添加它。

EDIT: 编辑:

This may shed light on the problem. 这可以揭示问题所在。 It may be because I'm running the wrong version? 可能是因为我运行了错误的版本? I also am getting this error: 我也收到此错误:

WARNING: The runtime version supported by this application is unavailable. 警告:此应用程序支持的运行时版本不可用。 Using default runtime: v4.0.30319 使用默认运行时:v4.0.30319

Should I be running version 3.5 for .NET 3.5? 我应该运行.NET 3.5的3.5版吗?

Please try the below code snippet. 请尝试以下代码片段。

   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;
}

I've faced this situation once I tried bundling my application with mono runtime to be used on machines that have no runtime installed out of the box. 一旦我尝试将应用程序与Mono Runtime捆绑在一起以在没有开箱即用安装的机器上使用时,我就遇到了这种情况。

the solution was to add App.config file to my project with the following contents (copied sections from machine.config file of my installed mono version) 解决方案是使用以下内容将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>

Hope this will give you a clue! 希望这会给你一个线索!

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

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