当我将get请求发送到Web时,如何在c#中获取重定向响应

时间:2017-08-11 02:21:35

标签: javascript c# winforms

我尝试向网络发送获取请求[A]。我可以从网上收到回复[A]。响应[A]是一个HTML,它有一个javascript,用于在加载页面时通过post方法提交表单[B]。现在,我想发送get请求[A]并接收最后一个表单响应的响应[B]。由于javascript有一些复杂的计算,我不想在c#中重写逻辑。

当我发送获取请求[A]时,如何收到表单的回复[B]?

HttpWebResponse response;
string strhtml;
CookieCollection cook;
cook =  new CookieCollection();
response = HttpWebResponseUtility.CreateGetHttpResponse(url, null, "", 
cookie);
StreamReader sr = new StreamReader(response.GetResponseStream());
strhtml  = sr.ReadToEnd();
//I can receive the response [A] which is a HTML.
sr.Close();
response.Close();



public static HttpWebResponse CreateGetHttpResponse(string url, int? 
timeout, string userAgent, CookieCollection cookies)
    {
        if (string.IsNullOrEmpty(url))
        {
            throw new ArgumentNullException("url");
        }
        HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
        request.Method = "GET";
        request.UserAgent = DefaultUserAgent;
        if (!string.IsNullOrEmpty(userAgent))
        {
            request.UserAgent = userAgent;
        }
        if (timeout.HasValue)
        {
            request.Timeout = timeout.Value;
        }
        if (cookies != null)
        {
            request.CookieContainer = new CookieContainer();
            request.CookieContainer.Add(cookies);
        }
        return request.GetResponse() as HttpWebResponse;
    }

`

0 个答案:

没有答案