C#/ Restsharp:HTTP错误417期望失败

时间:2015-07-04 12:36:23

标签: c# http restsharp

我有这段代码:

System.Net.ServicePointManager.Expect100Continue = false;

RestClient = new RestClient(baseUri);
RestClient.Proxy = new WebProxy("http://proxy.domain.tld:8888");
RestClient.Proxy.Credentials = new NetworkCredential("abc", "abc");
RestClient.CookieContainer = new System.Net.CookieContainer();

// Receive JSESSIONID
var req = new RestRequest("scs/result/resultinfo.jsp", Method.GET);
var res = RestClient.Execute(req); // This request works

// Login
req = new RestRequest("scs/result/j_security_check", Method.POST);
req.AddHeader("Content-Type", "application/x-www-form-urlencoded");

req.AddParameter("j_username", username, ParameterType.GetOrPost);
req.AddParameter("j_password", password, ParameterType.GetOrPost);

res = RestClient.Execute(req); // This request fails

POST请求导致HTTP错误417:期望失败,甚至System.Net.ServicePointManager.Expect100Continue = false

你能救我吗?感谢。

2 个答案:

答案 0 :(得分:1)

将XML代码放在我的解决方案中的每个App.config上都有帮助。

  <system.net>
    <settings>
      <servicePointManager expect100Continue="false" />
    </settings>
  </system.net>

答案 1 :(得分:0)

我从未使用过RestSharp,直到有一个新的项目来到我的路上。

StatusCode =  ExpectationFailed   

所以这是417期望失败的错误。太糟糕了,排除故障并不容易。

问题是 machine.config 中缺少 connectionstring (项目没有使用web.config / app.config来处理好几件事)

为我固定。