using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
string strId = "guest";
string strPassword = "123456"; string postData = "login_username=" + strId + "&login_loginpass=" + strPassword + "&aaa=%E7%99%BB%E5%BD%95&login=yes&can_input=0&u=&login_start_time=1359428826&tpl=&tn=&pu=&ssid=&from=&bd_page_type=&uid=1359428826232_510&login_username_input=0&type="
;
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://wappass.baidu.com/passport/"); }
}
}
c#
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
string strId = "guest";
string strPassword = "123456"; string postData = "login_username=" + strId + "&login_loginpass=" + strPassword + "&aaa=%E7%99%BB%E5%BD%95&login=yes&can_input=0&u=&login_start_time=1359428826&tpl=&tn=&pu=&ssid=&from=&bd_page_type=&uid=1359428826232_510&login_username_input=0&type="
;
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://wappass.baidu.com/passport/"); }
}
}
c#
using System;
using System.IO;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;namespace WPCheck
{
public class HttpHelper
{
public string contentType = "application/x-www-form-urlencoded";
public string accept = "text/html, application/xhtml+xml, */*";
public string userAgent = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)";
public string referer = string.Empty; [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool InternetSetCookie(string lpszUrl, string lpszCookieName, string lpszCookieData); /// <summary>
/// 获取流数据
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public Stream GetStream(string url, CookieContainer cookies = null)
{
cookies = cookies ?? new CookieContainer();
HttpWebRequest httpWebRequest = null;
HttpWebResponse httpWebResponse = null; try
{
httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
httpWebRequest.CookieContainer = cookies;
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 string GetHtml(string url, CookieContainer cookies = null, string method = "GET", string postData = "")
{
cookies = cookies ?? new CookieContainer();
HttpWebRequest httpWebRequest = null;
HttpWebResponse httpWebResponse = null; try
{
httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
httpWebRequest.CookieContainer = cookies;
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" && postData.Length > 0)
{
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 ex)
{
return ex.Message;
}
}
}
}
发一个我用的你参考参考。
你这代码至少也得来一句myRequest.GetResponse();才行啊注意这是请求头。
你可以参考参考,希望能帮到你~