问题1.但是获取的源代码总是登录页面的,请大家看看怎么才能获得登录后页面的源代码
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static string Post(string url, string indata)
{
CookieContainer myCookieContainer = new CookieContainer();
string outdata = "";
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
myHttpWebRequest.AllowAutoRedirect = true;
myHttpWebRequest.KeepAlive = true;
myHttpWebRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
myHttpWebRequest.Referer = "http://www.renren.com/";
myHttpWebRequest.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
myHttpWebRequest.Method = "POST";
myHttpWebRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.0; rv:11.0) Gecko/20100101 Firefox/11.0";
//myHttpWebRequest.ContentLength = indata.Length;
myHttpWebRequest.CookieContainer = myCookieContainer;
Stream myRequestStream = myHttpWebRequest.GetRequestStream();
StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.UTF8);
//ASCIIEncoding encoding = new ASCIIEncoding(); byte[] data = encoding.GetBytes(formData);
myStreamWriter.Write(indata, 0, indata.Length);
myStreamWriter.Close();
myRequestStream.Close();
HttpWebResponse myHttpResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
myHttpResponse.Cookies = myCookieContainer.GetCookies(myHttpWebRequest.RequestUri);
Stream myResponseStream = myHttpResponse.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
outdata = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
//Console.WriteLine(outdata);
url = "http://www.renren.com/*********";
myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
myHttpWebRequest.AllowAutoRedirect = true;
myHttpWebRequest.CookieContainer = myCookieContainer;
myHttpResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
myHttpResponse.Cookies = myCookieContainer.GetCookies(myHttpWebRequest.RequestUri);
myResponseStream = myHttpResponse.GetResponseStream();
myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
//outdata = myStreamReader.ReadToEnd();
return outdata;
}
private void button1_Click(object sender, EventArgs e)
{
string a = "http://www.renren.com";
string b = "**************";//postdata
textBox1.Text = Post(a, b);
System.Diagnostics.Process.Start(@"C:\Program Files\Internet Explorer\iexplore.exe", "http://www.renren.com/********");
}
public void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
myHttpResponse.Cookies = myCookieContainer.GetCookies(myHttpWebRequest.RequestUri);
你看一下 這個方法 有沒有cookies返回呢?
如果寫對了,應該 能登陸成功的,並且有數據的
看了一下,没有cookies返回值,应该怎么写?
@super 86: 你 藏考 http://blog.csdn.net/vip__888/article/details/5646260
@無限遐想: post地址写错了,尴尬,不过还是谢谢你了!!
还有个问题:http://www.renren.com/profile.do?portal=profileFriendlist&id=*********从字符串里匹配出前面这个字符串的正则表达式你会写吗?
星号部分为数字。
using System; using System.IO; using System.Net; using System.Text; namespace Examples.System.Net { public class WebRequestGetExample { public static void Main() { // Create a request for the URL. WebRequest request = WebRequest.Create("http://www.contoso.com/default.html"); // If required by the server, set the credentials. request.Credentials = CredentialCache.DefaultCredentials; // Get the response. HttpWebResponse response = (HttpWebResponse)request.GetResponse(); // Display the status. Console.WriteLine(response.StatusDescription); // Get the stream containing content returned by the server. Stream dataStream = response.GetResponseStream(); // Open the stream using a StreamReader for easy access. StreamReader reader = new StreamReader(dataStream); // Read the content. string responseFromServer = reader.ReadToEnd(); // Display the content. Console.WriteLine(responseFromServer); // Cleanup the streams and the response. reader.Close(); dataStream.Close(); response.Close(); Console.ReadKey(); } } }
你写的这个好像不需要登录验证的网站才可以吧?
同意楼上