Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# URL在浏览器中工作,但使用HttpWebRequest从控制台应用程序返回500_C#_Httpwebrequest_Console Application - Fatal编程技术网

C# URL在浏览器中工作,但使用HttpWebRequest从控制台应用程序返回500

C# URL在浏览器中工作,但使用HttpWebRequest从控制台应用程序返回500,c#,httpwebrequest,console-application,C#,Httpwebrequest,Console Application,尝试访问web服务URL时,可以正常工作(按预期返回状态代码200),但在通过控制台应用程序执行时,返回状态代码500?我怀疑是代理或DNS问题,但不确定 以下是从控制台应用程序创建的请求: // webRequestUrl has been modified const string webRequestUrl = "http://0.0.0.0/communication/?id={0}&status=70" var webRequest = WebRequest.Create(s

尝试访问web服务URL时,可以正常工作(按预期返回状态代码200),但在通过控制台应用程序执行时,返回状态代码500?我怀疑是代理或DNS问题,但不确定

以下是从控制台应用程序创建的请求:

// webRequestUrl has been modified
const string webRequestUrl = "http://0.0.0.0/communication/?id={0}&status=70"

var webRequest = WebRequest.Create(string.Format(webRequestUrl, invalidSseLead.LeadId));

try
{
    using (var webResponse = (HttpWebResponse)webRequest.GetResponse())
    {
        if (webResponse != null && webResponse.StatusCode == HttpStatusCode.OK)
        {
            using (var textStream = new StreamWriter("updateQuery.sql", true))
            {
                // Write some text
            }
        }
    }
}
catch (Exception exception)
{
    // Report exception
}
已经检查了生成的请求URL,这很好,不明白为什么它可以从浏览器而不是控制台应用程序工作


谢谢:)

该网站是否拒绝编程访问,以至于您可能不得不使用以下内容伪造您的用户代理

webRequest.UserAgent = "some normal browser user agent string";

谢谢你的帮助。事实证明,这个问题实际上是URL的问题,一个运行正常,但是另一个生成的URL引发了异常

+1到MSMS-我确实需要复制并粘贴生成的确切URL,而不是将其与测试数据一起使用


谢谢

如果你抓取字符串的结果。格式化并粘贴到浏览器中,它能工作吗?是的,已经测试过了,它能工作没有问题:(感谢帮助,尝试设置用户代理,但仍然返回相同的异常