WebRequest.Create 问题

发布于 2024-08-26 20:22:08 字数 2064 浏览 7 评论 0原文

我的要求是下载一个 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());
}

My requirement is downlaoding a HTTM page. Like and I am using WebRequest.Create.
But the line

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

Is throwing an exception {"Configuration system failed to initialize"}.
I am working in a compmany. Does it due to proxy or anything? But it’s occurring while creation of the URL it self.

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)

Code is like

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

长不大的小祸害 2024-09-02 20:22:08

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

<?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>

The method System.Net.Configuration.WebRequestModulesSectionInternal.GetSection is looking for a section called "webRequestModules" here is mine in my machine.config (WINDOWS/Microsft.net/Framework/your_version/config/machine.config)

<?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>
难以启齿的温柔 2024-09-02 20:22:08

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

From the exception message and the stack trace, it looks like somehow you're accessing Configuration. Try adding a reference to System.Configuration if you haven't got one.

岛歌少女 2024-09-02 20:22:08

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

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

The problem is within your .NET config, config is placed at many location, there is first machine.config, then there is config specific to the user and then there is config specific to the application, now if your company's domain controller has set your config wrongly or any of your machine.config or such config have wrong information, that can lead to this problem.

Try looking into your app.config if you see you mistyped anything in there.

三五鸿雁 2024-09-02 20:22:08

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

希望这有帮助。

I had the very same problem, which was weird because in one solution the same lines worked perfectly, while in the other one I got the error above. That means that the problem is with the local project settings file.
I solved it by copying the default Settings.settings and Settings.Designer.cs to the non-working project's Properties folder, rewriting the files there (after backing them up) and excluding the app.config file from the project.

Hope this helps.

〃安静 2024-09-02 20:22:08

我遇到了同样的问题,两个项目具有相同的代码,但一个有效,另一个无效。我将 App.config 从完美的项目复制并替换到有问题的项目,然后重建项目。

希望这有帮助...

I had the same problem, two projects with the same code but one worked and the other didn't. I copied and replaced the App.config from flawless project to the troubled one, and then rebuild the project.

Hope this helps ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文