C# http返回Cookie以后使用
1 public static string GetCookie(string requestUrlString, Encoding encoding, ref CookieContainer cookie) 2 { 3 //向服务端请求 4 HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(requestUrlString); 5 myRequest.ContentType = "application/x-www-form-urlencoded"; 6 myRequest.CookieContainer = new CookieContainer(); 7 //将请求的结果发送给客户端(界面、应用) 8 HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse(); 9 cookie.Add(myResponse.Cookies); 10 StreamReader reader = new StreamReader(myResponse.GetResponseStream(), encoding); 11 return reader.ReadToEnd(); 12 } 13 14 public static string GetHtml(string requestUrlString, Encoding encoding, CookieContainer cookie) 15 { 16 string ua = "Mozilla/5.0 (Linux; U; Android 2.2; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1"; 17 HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(requestUrlString); 18 myRequest.ContentType = "application/x-www-form-urlencoded"; 19 myRequest.UserAgent = ua; 20 myRequest.CookieContainer = cookie; 21 HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse(); 22 StreamReader reader = new StreamReader(myResponse.GetResponseStream(), encoding); 23 return reader.ReadToEnd(); 24 } 25 26 public static string PostLogin(string postData, string requestUrlString, ref CookieContainer cookie) 27 { 28 ASCIIEncoding encoding = new ASCIIEncoding(); 29 byte[] data = encoding.GetBytes(postData); 30 //向服务端请求 31 HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(requestUrlString); 32 myRequest.Method = "POST"; 33 myRequest.ContentType = "application/x-www-form-urlencoded"; 34 myRequest.ContentLength = data.Length; 35 myRequest.CookieContainer = new CookieContainer(); 36 Stream newStream = myRequest.GetRequestStream(); 37 newStream.Write(data, 0, data.Length); 38 newStream.Close(); 39 //将请求的结果发送给客户端(界面、应用) 40 HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse(); 41 cookie.Add(myResponse.Cookies); 42 StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8); 43 return reader.ReadToEnd(); 44 } 45 46 public static string PostRequest(string postData, string requestUrlString, CookieContainer cookie) 47 { 48 ASCIIEncoding encoding = new ASCIIEncoding(); 49 byte[] data = encoding.GetBytes(postData); 50 HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(requestUrlString); 51 myRequest.Method = "POST"; 52 myRequest.ContentType = "application/x-www-form-urlencoded"; 53 myRequest.ContentLength = data.Length; 54 myRequest.CookieContainer = cookie; 55 Stream newStream = myRequest.GetRequestStream(); 56 newStream.Write(data, 0, data.Length); 57 newStream.Close(); 58 HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse(); 59 StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8); 60 return reader.ReadToEnd(); 61 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗