开发者

WebRequest.Create problem

开发者 https://www.devze.com 2022-12-24 13:35 出处:网络
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\");

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.mayos开发者_运维问答oftware.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());
}


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>


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.


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.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消