首页 新闻 搜索 专区 学院

获取需要登陆的网页的源代码C#

0
悬赏园豆:50 [已解决问题] 解决于 2012-04-26 15:24

问题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)
        {

        }
    }
}

super 86的主页 super 86 | 初学一级 | 园豆:11
提问于:2012-04-25 23:08
< >
分享
最佳答案
0

myHttpResponse.Cookies = myCookieContainer.GetCookies(myHttpWebRequest.RequestUri);

你看一下 這個方法 有沒有cookies返回呢?

如果寫對了,應該 能登陸成功的,並且有數據的

收获园豆:50
無限遐想 | 老鸟四级 |园豆:3740 | 2012-04-26 09:29

看了一下,没有cookies返回值,应该怎么写?

super 86 | 园豆:11 (初学一级) | 2012-04-26 09:34

@super 86: 你 藏考 http://blog.csdn.net/vip__888/article/details/5646260

無限遐想 | 园豆:3740 (老鸟四级) | 2012-04-26 09:35

@無限遐想: post地址写错了,尴尬,不过还是谢谢你了!!

还有个问题:http://www.renren.com/profile.do?portal=profileFriendlist&id=*********从字符串里匹配出前面这个字符串的正则表达式你会写吗?

星号部分为数字。

super 86 | 园豆:11 (初学一级) | 2012-04-26 11:07
其他回答(2)
0
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();
        }
    }
}
Halower | 园豆:1723 (小虾三级) | 2012-04-26 00:36

你写的这个好像不需要登录验证的网站才可以吧?

支持(0) 反对(0) super 86 | 园豆:11 (初学一级) | 2012-04-26 08:48
0

同意楼上

韩天伟 | 园豆:697 (小虾三级) | 2012-04-26 08:33
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册