惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

罗磊的独立博客
Recent Announcements
Recent Announcements
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
有赞技术团队
有赞技术团队
J
Java Code Geeks
T
The Blog of Author Tim Ferriss
MyScale Blog
MyScale Blog
人人都是产品经理
人人都是产品经理
aimingoo的专栏
aimingoo的专栏
U
Unit 42
The GitHub Blog
The GitHub Blog
云风的 BLOG
云风的 BLOG
T
Tailwind CSS Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 三生石上(FineUI控件)
Apple Machine Learning Research
Apple Machine Learning Research
小众软件
小众软件
Hugging Face - Blog
Hugging Face - Blog
博客园 - 司徒正美
腾讯CDC
I
InfoQ
GbyAI
GbyAI
博客园_首页

博客园 - 偶然微笑

asp.net获取URL和IP地址 ASP.NET获取IP与MAC地址的方法 ASP.NET获取IP的6种方法 - 偶然微笑 - 博客园 MS SQL Server 2005 通用分页存储过程 又快又简单的sql2005分页存储过程 SQL SERVER 2005分页存储过程 C#区别和认识四个判等函数 C#中数字日期转中文日期 C#算法(一)选择排序 创建基于ASP.NET的SMTP邮件服务 url传递中文的解决方案总结 C#如何取硬件标志 使用HttpWebRequest提交ASP.NET表单并保持Session和Cookie 提取HTML代码中文字的C#函数 Asp.Net输出数据到EXCEL中 把文字变成图片的小程序 ASP.NET URL Rewrite. URL重写 asp.net采集函数(采集、分析、替换、入库) .net 无限级分类
asp.net 登陆
偶然微笑 · 2008-09-24 · via 博客园 - 偶然微笑

网上搜了很多代码段,但是都没有调试成功,以下这一段顺利通过了,记录下来备忘

 1        public static string PostData(string url,string indata,CookieContainer myCookieContainer)
 2        {
 3            string outdata=""
 4            HttpWebRequest myHttpWebRequest=(HttpWebRequest)WebRequest.Create(url); 
 5            myHttpWebRequest.ContentType="application/x-www-form-urlencoded"
 6            myHttpWebRequest.ContentLength=indata.Length; 
 7            myHttpWebRequest.Method="POST"
 8            myHttpWebRequest.CookieContainer=myCookieContainer; 
 9            Stream myRequestStream=myHttpWebRequest.GetRequestStream(); 
10            StreamWriter myStreamWriter=new StreamWriter(myRequestStream,Encoding.GetEncoding("gb2312"));                 
11            myStreamWriter.Write(indata); 
12            myStreamWriter.Close(); 
13            myRequestStream.Close(); 
14            HttpWebResponse myHttpWebResponse=(HttpWebResponse)myHttpWebRequest.GetResponse(); 
15            myHttpWebResponse.Cookies=myCookieContainer.GetCookies(myHttpWebRequest.RequestUri); 
16            Stream myResponseStream=myHttpWebResponse.GetResponseStream(); 
17            StreamReader myStreamReader=new StreamReader(myResponseStream,Encoding.GetEncoding("gb2312")); 
18            outdata=myStreamReader.ReadToEnd(); 
19            myStreamReader.Close(); 
20            myResponseStream.Close();
21            return outdata;
22        }

CookieContainer myCookieContainer=new CookieContainer(); 
string url="?????????????????";//登录页面的链接,看from里面的action
string loginfo="?????????????????";//登录字符串,例如user=??&pass=????
PostData(url,loginfo,myCookieContainer);//这个返回的应该是登录成功的信息,而且写好cookie了

url
="???????????";//这个就是想要读取的页面地址,普通情况用StreamReader是读不到的喔
PostData(url,"",myCookieContainer);//返回的就是目标页面,完成,如果还要连续读其它页面,继续这两句代码就行了(cookie已经保存了,自己用就好了).