应用c#读取带cookie的http数据详解编程语言

@(编程)

private static string Login() 
{ 
    string url = string.Format("{0}/login-submit.html?identity=teacher&loginName={1}&password={2}", Config.HOST, Config.USER, Config.PASSWORD); 
    HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; 
    try 
    { 
        using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) 
        { 
            StreamReader reader = new StreamReader(response.GetResponseStream()); 
            return response.Headers.Get("Set-Cookie"); 
        } 
    } 
    catch (Exception ex) 
    { 
        Console.WriteLine(ex.Message); 
        return errorMsg; 
    } 
} 
 
private static string GetMethod(string cookie) 
{ 
    string url = string.Format("{0}/teacher/student/employment-list.html", Config.HOST); 
    HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; 
    CookieContainer co = new CookieContainer(); 
    co.SetCookies(new Uri(url), cookie); 
 
    request.CookieContainer = co; 
    try 
    { 
        using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) 
        { 
            StreamReader reader = new StreamReader(response.GetResponseStream()); 
            return reader.ReadToEnd(); 
        } 
    } 
    catch (Exception ex) 
    { 
        Console.WriteLine(ex.Message); 
        return errorMsg; 
    } 
} 

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/20957.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论