2

我的要求是下载一个 HTTM 页面。就像我正在使用 WebRequest.Create。但是线

HttpWebRequest request = (HttpWebRequest) WebRequest.Create("http://www.mayosoftware.com");

正在引发异常{“配置系统初始化失败”}。我在一家公司工作。是因为代理还是什么?但它是在创建它自己的 URL 时发生的。

Exception trace is:

   at System.Configuration.ConfigurationManager.PrepareConfigSystem()
   at System.Configuration.ConfigurationManager.GetSection(String sectionName)
   at System.Configuration.PrivilegedConfigurationManager.GetSection(String sectionName)
   at System.Net.Configuration.WebRequestModulesSectionInternal.GetSection()
   at System.Net.WebRequest.get_PrefixList()
   at System.Net.WebRequest.Create(Uri requestUri, Boolean useUriBase)

代码就像

void GetHTTPReq()
{ 

Looking forward on it. The complete code is as follows but problem is in the starting itself
:


\\            // used to build entire input

        StringBuilder sb = new StringBuilder();

        // used on each read operation
        byte[] buf = new byte[8192];

        // prepare the web page we will be asking for
        HttpWebRequest request = (HttpWebRequest) WebRequest.Create("http://www.mayosoftware.com");

        // execute the request
        HttpWebResponse response = (HttpWebResponse)
            request.GetResponse();

        // we will read data via the response stream
        Stream resStream = response.GetResponseStream();

        string tempString = null;
        int count = 0;

        do
        {
            // fill the buffer with data
            count = resStream.Read(buf, 0, buf.Length);

            // make sure we read some data
            if (count != 0)
            {
                // translate from bytes to ASCII text
                tempString = Encoding.ASCII.GetString(buf, 0, count);

                // continue building the string
                sb.Append(tempString);
            }
        }
        while (count > 0); // any more data to read?

        // print out page source
        Console.WriteLine(sb.ToString());
}
4

5 回答 5

2

System.Net.Configuration.WebRequestModulesSectionInternal.GetSection 方法在我的 machine.config (WINDOWS/Microsft.net/Framework/your_version/config/machine.config) 中寻找一个名为“webRequestModules”的部分

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<configSections>
...
  <sectionGroup name="system.net" type="System.Net.Configuration.NetSectionGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
    ...
    <section name="webRequestModules" type="System.Net.Configuration.WebRequestModulesSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
    ...
  </sectiongroup>
...
</configsection>
...
</configuration>
于 2010-03-26T10:53:27.087 回答
0

从异常消息和堆栈跟踪来看,您似乎正在以某种方式访问​​配置。System.Configuration如果没有,请尝试添加参考。

于 2010-03-26T09:42:06.970 回答
0

问题出在您的 .NET 配置中,配置放置在很多位置,首先是 machine.config,然后是特定于用户的配置,然后是特定于应用程序的配置,现在如果您公司的域控制器设置了您的配置错误或您的任何 machine.config 或此类配置有错误信息,都可能导致此问题。

如果您发现您在其中输入了错误的任何内容,请尝试查看您的 app.config。

于 2010-03-26T09:45:37.333 回答
0

我遇到了同样的问题,这很奇怪,因为在一个解决方案中,相同的行完美运行,而在另一个解决方案中,我得到了上面的错误。这意味着问题出在本地项目设置文件上。我通过将默认的 Settings.settings 和 Settings.Designer.cs 复制到非工作项目的 Properties 文件夹,在那里重写文件(在备份它们之后)并从项目中排除 app.config 文件来解决它。

希望这可以帮助。

于 2010-04-27T13:13:13.380 回答
0

我有同样的问题,两个项目具有相同的代码,但一个工作,另一个没有。我将App.config从完美项目复制并替换为有问题的项目,然后重新构建项目。

希望这可以帮助 ...

于 2019-08-25T07:58:56.633 回答