C#模拟http 发送post或get请求
来源:互联网 发布:淘宝宝贝定时上架 编辑:程序博客网 时间:2024/04/28 04:30
private string HttpPost(string Url, string postDataStr) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = Encoding.UTF8.GetByteCount(postDataStr); request.CookieContainer = cookie; Stream myRequestStream = request.GetRequestStream(); StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312")); myStreamWriter.Write(postDataStr); myStreamWriter.Close(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); response.Cookies = cookie.GetCookies(response.ResponseUri); Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")); string retString = myStreamReader.ReadToEnd(); myStreamReader.Close(); myResponseStream.Close(); return retString; } public string HttpGet(string Url, string postDataStr) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr == "" ? "" : "?") + postDataStr); request.Method = "GET"; request.ContentType = "text/html;charset=UTF-8"; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")); string retString = myStreamReader.ReadToEnd(); myStreamReader.Close(); myResponseStream.Close(); return retString; }
在post的时候有时也用的到cookie,像登录163发邮件时候就需要发送cookie,所以在外部一个cookie属性随时保存 CookieContainer cookie = new CookieContainer();
!注意:有时候请求会重定向,但我们就需要从重定向url获取东西,像QQ登录成功后获取sid,但上面的会自动根据重定向地址跳转。我们可以用:
request.AllowAutoRedirect = false;设置重定向禁用,你就可以从headers的Location属性中获取重定向地址
- C#模拟http 发送post或get请求
- C#模拟http 发送post或get请求
- C#模拟http 发送post或get请求
- C#模拟http 发送post或get请求
- C#模拟http 发送post或get请求
- C#模拟http 发送post或get请求
- C#模拟http 发送post或get请求
- C#模拟http 发送post或get请求
- C#模拟http 发送POST或GET请求
- C#模拟http 发送post或get请求
- C#模拟http 发送post或get请求
- curl模拟http发送get或post请求
- C#模拟发送http get、post请求的方式
- c#发送http请求get post
- 发送post或get请求
- 发送GET或POST请求
- 模拟Http发送get、post、put、delete请求
- java模拟发送http的get、post请求
- ASP.NET操作XML
- Code First Entity Framework基本与最佳添加(add/create),删除(delete/remove),修改(update/modify)操作
- C#数组学习实例
- 三种SQL分页法
- fedora 17 安装 skyeye1.3.5
- C#模拟http 发送post或get请求
- C# POST模拟登录发贴
- Keil 使用 Jlink 进行调试出现错误: No Algorithm found for: 08000000H - 08001557H错误的解决
- C#判断用户是手机访问还是PC访问
- Java enum valueof 枚举必须大小写一致 否则throw exception
- 第二章:入门(YII)
- 第三章:TrackStar应用程序(YII)
- 第四章:创建初始应用---连接数据库(YII)
- 第五章:CRUD的操作(YII)