如图所示,我模拟小米登录,但是Post数据总是重复的,如何解决这个问题
这是我的代码private void aa(string url, string cookie, string post, Encoding encode)
{
byte[] bs = encode.GetBytes(post);
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:15.0) Gecko/20100101 Firefox/15.0";
req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
req.Headers[HttpRequestHeader.AcceptEncoding] = "gzip, deflate";
req.Headers[HttpRequestHeader.AcceptLanguage] = "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3";
req.Referer = "https://account.xiaomi.com/pass/serviceLogin"; CookieContainer co = new CookieContainer();
co.SetCookies(new Uri(url), cookie);
req.CookieContainer = co; req.ContentLength = bs.Length; using (Stream reqStream = req.GetRequestStream())
{
reqStream.Write(bs, 0, bs.Length);
}
using (WebResponse wr = req.GetResponse())
{
//在这里对接收到的页面内容进行处理
string setCookie = wr.Headers[HttpResponseHeader.SetCookie];
MessageBox.Show(setCookie);
}
}
这是我的代码private void aa(string url, string cookie, string post, Encoding encode)
{
byte[] bs = encode.GetBytes(post);
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:15.0) Gecko/20100101 Firefox/15.0";
req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
req.Headers[HttpRequestHeader.AcceptEncoding] = "gzip, deflate";
req.Headers[HttpRequestHeader.AcceptLanguage] = "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3";
req.Referer = "https://account.xiaomi.com/pass/serviceLogin"; CookieContainer co = new CookieContainer();
co.SetCookies(new Uri(url), cookie);
req.CookieContainer = co; req.ContentLength = bs.Length; using (Stream reqStream = req.GetRequestStream())
{
reqStream.Write(bs, 0, bs.Length);
}
using (WebResponse wr = req.GetResponse())
{
//在这里对接收到的页面内容进行处理
string setCookie = wr.Headers[HttpResponseHeader.SetCookie];
MessageBox.Show(setCookie);
}
}
解决方案 »
- silverlight的xaml文件之间的相互调用
- 急求解啊:成员名称不能与它们的封闭类型相同!!!
- .net线程
- ****求高手分享,问一个小问题****
- 把jsb格式文件的内容在C#中显示出来!
- 用SerialPort做短信收发的几个问题请教
- c# datatable插入数据库
- 菜鸟请教程序中用到ShowMessage("...")时,开头该用using +? 谢谢
- 关于dev 的treelist,问两个简单的问题。
- 如图;我想把图片保存在右边解决方案的Resources文件夹里(服务器端),但它报GDI错误,是因为路径错误的原因吗??还是因为别的?求高手解决...谢谢谢谢.
- 计算最小距离
- WPF/SL 中得依赖属性问题
但是抓取显示的是两次
string post = "passToken=&user=********&pwd=*********&callback=https%3A%2F%2Faccount.xiaomi.com&sid=passport&hidden=";这个是我传递过去的Post
public class HttpHelper
{
public static string contentType = "application/x-www-form-urlencoded";
public static string accept = "text/html, application/xhtml+xml, */*";
public static string userAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; BOIE9;ZHCN)";
public static string referer = string.Empty; /// <summary>
/// 获取字符流
/// </summary>
/// <param name="url"></param>
/// <param name="cookieContainer"></param>
/// <returns></returns>
public static Stream GetStream(string url, CookieContainer cookieContainer)
{
HttpWebRequest httpWebRequest = null;
HttpWebResponse httpWebResponse = null; try
{
httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
httpWebRequest.CookieContainer = cookieContainer;
httpWebRequest.ContentType = contentType;
httpWebRequest.Referer = referer;
httpWebRequest.Accept = accept;
httpWebRequest.UserAgent = userAgent;
httpWebRequest.Method = "GET";
httpWebRequest.ServicePoint.ConnectionLimit = int.MaxValue; httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
Stream responseStream = httpWebResponse.GetResponseStream(); return responseStream;
}
catch (Exception)
{
return null;
}
} /// <summary>
/// 获取HTML
/// </summary>
/// <param name="url"></param>
/// <param name="cookieContainer"></param>
/// <param name="method">GET or POST</param>
/// <param name="postData">like "username=admin&password=123"</param>
/// <returns></returns>
public static string GetHtml(string url, CookieContainer cookieContainer = null, string method = "GET", string postData = "")
{
cookieContainer = cookieContainer ?? new CookieContainer(); HttpWebRequest httpWebRequest = null;
HttpWebResponse httpWebResponse = null; try
{
httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
httpWebRequest.CookieContainer = cookieContainer;
httpWebRequest.ContentType = contentType;
httpWebRequest.Referer = referer;
httpWebRequest.Accept = accept;
httpWebRequest.UserAgent = userAgent;
httpWebRequest.Method = method;
httpWebRequest.ServicePoint.ConnectionLimit = int.MaxValue;
httpWebRequest.AllowAutoRedirect = true; if (method.ToUpper() == "POST")
{
byte[] byteRequest = Encoding.UTF8.GetBytes(postData);
Stream stream = httpWebRequest.GetRequestStream();
stream.Write(byteRequest, 0, byteRequest.Length);
stream.Close();
} httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
Stream responseStream = httpWebResponse.GetResponseStream();
StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8);
string html = streamReader.ReadToEnd(); streamReader.Close();
responseStream.Close(); httpWebRequest.Abort();
httpWebResponse.Close(); return html;
}
catch (Exception)
{
return string.Empty;
}
}
}
static void Main(string[] args)
{
string result = HttpHelper.GetHtml("https://account.xiaomi.com/pass/serviceLoginAuth", null, "POST", "passToken=&user=*******@qq.com&pwd=dafsd3df3&callback=https%3A%2F%2Faccount.xiaomi.com&sid=passport&hidden=");
}
我试了一下,一切正常。上面是我测试的代码。
其中注意设置:httpWebRequest.AllowAutoRedirect = true;
因为这处理登陆请求的时候url转发了3次,如果不设置自动重定向的话,自己就得处理多次。