【问题标题】:the remote server returned an error (400) bad request远程服务器返回错误(400)错误请求
【发布时间】:2018-06-25 14:14:32
【问题描述】:

我正在尝试使用 RSS 提要 URL 生成 XML 格式的数据。我得到了例外:

远程服务器返回错误 (400) 错误请求错误。

我使用的是 SSIS 包,我在控制流中创建了一个安全任务,我编写的脚本如下:

public bool DownloadFeed()
{
    string user = "xxx";
    string password = "pwd";

    WebClient web = new WebClient();
    System.Net.WebClient wc = new System.Net.WebClient();
    wc.Credentials = new System.Net.NetworkCredential(user, password);
    System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Ssl3 | 
                                                        SecurityProtocolType.Tls | 
                                                        SecurityProtocolType.Tls11 | 
                                                        SecurityProtocolType.Tls12;
    wc.DownloadFile(@"https://Entered RSS Feed URL here", @"H:\import\Test.xml");
    return true;
}

【问题讨论】:

  • 你试过没有 System.Net.ServicePointManager.SecurityProtocol
  • 嗨。是的,我在得到之前尝试过请求被中止:无法创建 SSL/TLS 安全通道。错误。所以我在代码中添加了 System.Net.ServicePointManager.SecurityProtocol 行
  • 可能缺少标题;见:stackoverflow.com/questions/22623664/…
  • 非常感谢..它按预期工作

标签: c# webclient


【解决方案1】:

这可能是凭据被格式化并传递给服务器的方式。 你可以试试这个吗?

using System.Net;

public bool DownloadFeed()
{
    string user = "xxx";
    string password = "pwd";

    System.Net.WebClient wc = new System.Net.WebClient();
    string credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes(user + ":" + password));
    wc.Headers[HttpRequestHeader.Authorization] = "Basic " + credentials;
    System.Net.ServicePointManager.SecurityProtocol =      System.Net.SecurityProtocolType.Ssl3 | 
                                                    SecurityProtocolType.Tls | 
                                                    SecurityProtocolType.Tls11 | 
                                                    SecurityProtocolType.Tls12;
    wc.DownloadFile(@"https://Entered RSS Feed URL here", @"H:\import\Test.xml");
    return true;
}

【讨论】:

  • 感谢您的回复。 got 'object' 不包含 'Headers' 的定义,并且在 wc.Headers 行找不到接受“object”类型的第一个参数的扩展方法“Headers”(您是否缺少 using 指令或程序集引用?) [HttpRequestHeader.Authorization] = "基本" + 凭据;
  • 当您使用 WebClient 的完全限定名称时,我怀疑您可能缺少 using 指令。我已经更新了上面的代码以包含缺少的指令。
  • 非常感谢..工作正常
猜你喜欢
  • 2018-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-21
  • 1970-01-01
相关资源
最近更新 更多