c# - WebRequest.Create 问题

标签 c# httpwebrequest webrequest

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

最佳答案

方法 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>

关于c# - WebRequest.Create 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2522160/

相关文章:

C# - 当 webRequest 发送 XML 时,将 Base64 安全为 Gif?

c# - 自定义项目类型的 "Start button"的自定义行为

c# - Azure Application Insights 不显示在 Azure 函数中创建的自定义事件

c# - 当通过 .NET Compact 从 C# 向同一 Java 服务器发出两个 HttpWebRequest 时,出现 "Request timed out"

C# HttpWebRequest 验证/指定使用的密码

c# - UnityWebRequest 返回 "\u001f�\b"

.net - 为什么默认情况下不启用 PreAuthenticate?

c# - 如何在 Nsubstitute 中使用内部服务?

c# - 如何删除XML中未关闭的节点?

coldfusion - 查看 cfhttp 请求