2

问题:我们有一个非常慢的工作流程,我正在尝试使用 RestSharp(我以前从未使用过)在 restful / MVC3 中重写它。到目前为止,我能够执行网站的初始 GET,但是当我进行第一次后续 POST(模拟单击复选框,然后单击继续按钮转到下一页)时,我不断得到以下错误:405 - HTTP verb used to access this page is not allowed. 无法显示您要查找的页面,因为使用了无效的方法(HTTP 动词)尝试访问。

我可能在代码中做错了什么。我知道我试图“屏幕抓取”的网站允许这些类型的请求,因为我们有一个古老的缓慢工作流程基础,目前正在运行,但它非常缓慢且过时。旁注该网站位于 asp.net 中,需要会话状态。

这是我的代码:

var client = new RestClient("https://thewebsite.com/");            
var request = new RestRequest(Method.GET);
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");            
request.RequestFormat = DataFormat.Json;

var response = client.Execute(request);

string responseString = string.Empty;
if (response.StatusCode == HttpStatusCode.OK)
{                                 
    string ViewState = string.Empty;
    string EventValidation = string.Empty;
    request = new RestRequest(Method.POST);    

    request.AddHeader("Content-Type", "application/x-www-form-urlencoded");                
    request.RequestFormat = DataFormat.Json;                
    responseString = HttpUtility.HtmlDecode(response.Content);

    request.AddParameter("__EVENTTARGET", "");
    request.AddParameter("__EVENTARGUMENT", "");
    request.AddParameter("ctl00%24MainContent%24cbAgree", "on");
    request.AddParameter("ctl00%24MainContent%24ctl01", "Continue");                

    //parses & adds the session variables from the raw html to keep the sessionstate
    Match m = RegexMatch(responseString, @"<input(\s\w+?[^=]*?=""[^""]*?"")*?\s+?id=""(\S+?\s)*?__VIEWSTATE(\s\S+?)*?"".*?value=""(?<value>.*?)"" />");
    if (m.Success)
        ViewState = m.Groups["value"].Value;

    m = RegexMatch(responseString, @"<input(\s\w+?[^=]*?=""[^""]*?"")*?\s+?id=""(\S+?\s)*?__EVENTVALIDATION(\s\S+?)*?"".*?value=""(?<value>.*?)"" />");
    if (m.Success)
        EventValidation = m.Groups["value"].Value;

    if (!string.IsNullOrWhiteSpace(ViewState) && !string.IsNullOrWhiteSpace(EventValidation))
    {
        request.AddParameter("__VIEWSTATE", UrlEncode(ViewState));
        request.AddParameter("__EVENTVALIDATION", UrlEncode(EventValidation));
    }

    response = client.Execute(request);
}
else
{
    //error in the GET
    responseString = response.StatusDescription + " --------------------" + HttpUtility.HtmlDecode(response.Content);
}
4

0 回答 0