using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
namespace weibo163APP
{
class Program
{
static void Main(string[] args)
{
A();
Console.Read();
}
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 = @"username=XXXXXXXXXX&password=XXXXXXXX&url=http%3A%2F%2Ft.163.com%2Ftag%2F%25E6%25B5%25B7%25E5%25B0%2594&product=t&type=1";//模拟请求数据
string LoginUrl = "https://reg.163.com/logins.jsp";
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.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;
Console.WriteLine(Cookiesstr);
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("gb2312"));
string content = sr.ReadToEnd();
response.Close();
}
catch (Exception)
{
//第一次POST出错;
}
try
{
//第一次POST请求
string postdata = @"k=mobile&o=&t=ALL&ta=al&pageNo=1&pageMethod=page";//模拟请求数据
string LoginUrl = "http://t.163.com/search.do?action=searchTweet";
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.AllowAutoRedirect = false;
request.CookieContainer = cc;
request.Headers.Add("Cookie:" + Cookiesstr);
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;
Console.WriteLine(Cookiesstr);
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("UTF-8"));
string content = sr.ReadToEnd();
Console.WriteLine(content);
response.Close();
}
catch (Exception)
{
//第一次POST出错;
}
try
{
//第一次POST请求
string postdata = @"k=mobile&o=&t=ALL&ta=al&pageNo=2&pageMethod=page";//模拟请求数据
string LoginUrl = "http://t.163.com/search.do?action=searchTweet";
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.AllowAutoRedirect = false;
request.CookieContainer = cc;
request.Headers.Add("Cookie:" + Cookiesstr);
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;
Console.WriteLine(Cookiesstr);
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("UTF-8"));
string content = sr.ReadToEnd();
Console.WriteLine(content);
response.Close();
}
catch (Exception)
{
//第一次POST出错;
}
}
}
}
搜狐测试代码
最新推荐文章于 2021-07-04 14:54:03 发布