博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POST、GET的测试代码
阅读量:2288 次
发布时间:2019-05-09

本文共 4537 字,大约阅读时间需要 15 分钟。

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Net;using System.IO;using System.Text.RegularExpressions;namespace SinaLoginApp{    class Class3    {        public static void A()        {            HttpWebRequest request = null;            HttpWebResponse response = null;            string gethost = string.Empty;            CookieContainer cc = new CookieContainer();            string Cookiesstr = string.Empty;                try            {                               //第一次POST请求                string postdata = @"entry=account&gateway=1&from=&savestate=0&useticket=0&vsnf=1&su=NTkxNzU3ODMzJTQwcXEuY29t&service=account                                  &sp=312tedijoteamo&encoding=UTF-8&prelt=0&callback=parent.sinaSSOController.loginCallBack&returntype=IFRAME                                  &setdomain=1";//模拟请求数据                string LoginUrl = "https://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.3.22)";                request = (HttpWebRequest)WebRequest.Create(LoginUrl);//实例化web访问类                request.Method = "POST";//数据提交方式为POST                //模拟头                request.ContentType = "application/x-www-form-urlencoded";                byte[] postdatabytes = Encoding.UTF8.GetBytes(postdata);                request.ContentLength = postdatabytes.Length;                request.Referer = "login.sina.com.cn";                request.AllowAutoRedirect = false;                request.CookieContainer = cc;                request.KeepAlive = true;                //提交请求                Stream stream;                stream = request.GetRequestStream();                stream.Write(postdatabytes, 0, postdatabytes.Length);                stream.Close();                //接收响应                response = (HttpWebResponse)request.GetResponse();                //保存返回cookie                response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);                CookieCollection cook = response.Cookies;                string strcrook = request.CookieContainer.GetCookieHeader(request.RequestUri);                Cookiesstr = strcrook;                //取第一次GET跳转地址                StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("gb2312"));                string content = sr.ReadToEnd();                response.Close();                                          }            catch (Exception)            {                //第一次POST出错;            }            try            {                gethost = "http://login.sina.com.cn/crossdomain2.php?action=logincallback&retcode=0&reason=&callback=parent.sinaSSOController.loginCallBack&setdomain=1"; //第一次GET跳转地址                request = (HttpWebRequest)WebRequest.Create(gethost);                request.Method = "GET";                request.KeepAlive = true;                request.Headers.Add("Cookie:" + Cookiesstr);                request.CookieContainer = cc;                request.AllowAutoRedirect = false;                response = (HttpWebResponse)request.GetResponse();                //设置cookie                   Cookiesstr = request.CookieContainer.GetCookieHeader(request.RequestUri);                //取再次跳转链接                   StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);                string content = sr.ReadToEnd();                request.Abort();                sr.Close();                response.Close();            }            catch (Exception)            {                //第一次GET出错               }            try            {                gethost = "http://login.sina.com.cn/member/my.php?entry=sso"; //第一次GET地址                request = (HttpWebRequest)WebRequest.Create(gethost);                request.Method = "GET";                request.KeepAlive = true;                request.Headers.Add("Cookie:" + Cookiesstr);                request.CookieContainer = cc;                request.AllowAutoRedirect = false;                response = (HttpWebResponse)request.GetResponse();                //设置cookie                   Cookiesstr = request.CookieContainer.GetCookieHeader(request.RequestUri);                //取再次跳转链接                   StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("gb2312"));                string content = sr.ReadToEnd();                request.Abort();                sr.Close();                response.Close();            }            catch (Exception)            {                //第一次GET出错               }           }    }}

转载地址:http://vyunb.baihongyu.com/

你可能感兴趣的文章
Linux终止进程
查看>>
VS2008使用Boost
查看>>
[转]bcp: 给boost瘦身
查看>>
[转]SQl中的左连接,右连接,内连接,全连接,自连接的理解
查看>>
[转]SQL语句的并集UNION,交集JOIN(内连接,外连接),交叉连接(CROSS JOIN笛卡尔积),差集(NOT IN)
查看>>
[转]数据库设计三大范式应用实例剖析
查看>>
[转]第14集 再探C++中异常的rethrow
查看>>
[转]C++异常机制的实现方式和开销分析
查看>>
找零问题
查看>>
[转]使用python调用计算所分词
查看>>
[转]C++各大有名科学计算库
查看>>
[转]BOOST 正则表达式 在Visual Studio 2005 下(VC8)的安装
查看>>
C++常用类型转换
查看>>
GNU GDB Debugger Command Cheat Sheet
查看>>
硬盘安装Ubuntu9.10
查看>>
Ubuntu安装 配置
查看>>
[转]linux和unix下常用解压和压缩命令
查看>>
Emacs Vim 常用命令
查看>>
正则表达式
查看>>
C++ Hash
查看>>