c# 模拟post登录
使用模拟登录大致可以分为两步
一、post登录获取cookis
public CookieContainer GetCookie(string url,string account,string password, out bool result)
{
CookieContainer cc = new CookieContainer();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
// Set the Method property of the request to POST.
request.Method = "POST";
// Create POST data and convert it to a byte array.
//string postData = "";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
// Set the ContentType property of the WebRequest.
//request.ContentType = "application/x-www-form-urlencoded";
request.ContentType = "application/x-www-form-urlencoded";
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
request.ProtocolVersion = HttpVersion.Version11;
request.AllowAutoRedirect = true;
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;
request.CookieContainer = cc;
// Get the request stream.
Stream dataStream = request.GetRequestStream();
// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close();
// Get the response.
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// Display the status.
//Console.WriteLine(((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
reader.Close();
dataStream.Close();
response.Close();
//判断登录是否成功
if (responseFromServer.Contains("您填写的账号或密码不正确,请再次尝试"))
result = false;
else
result = true;
return cc;
}
其中postdata可以通过网页工具如firebug获取到。找到其中关键处key value如用户名密码等
二、根据获取到的cookie获取网页内容或提交命令
1、根据cookis获取数据
public string GetHtmlDatas(string url, CookieContainer cc)
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.CookieContainer = cc;
webRequest.Method = "GET";
//Get the response from the server and save the cookies from the first request..
HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
// 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();
return responseFromServer;
}
2、根据登录成功的cookie操作post命令
public string PostCommand(string url,CookieContainer cc)
{
//http://xs.bgy.com.cn/Sale/RoomQuery/QureyRoomShowDetail.aspx?roomid=LHY038D-1-3101
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
// Set the Method property of the request to POST.
request.Method = "POST";
// Create POST data and convert it to a byte array.
string postData = ""
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
// Set the ContentType property of the WebRequest.
//request.ContentType = "application/x-www-form-urlencoded";
request.ContentType = "application/x-www-form-urlencoded";
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
request.ProtocolVersion = HttpVersion.Version11;
request.AllowAutoRedirect = true;
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;
request.CookieContainer = cc;
// Get the request stream.
Stream dataStream = request.GetRequestStream();
// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close();
// Get the response.
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// Display the status.
//Console.WriteLine(((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
reader.Close();
dataStream.Close();
response.Close();
return responseFromServer;
}
c# 模拟post登录的更多相关文章
- QQ模拟自动登录实现
QQ模拟自动登录实现 本篇文章主要介绍"QQ模拟自动登录实现(带验证码)",主要涉及到java 实现QQ自动登录(带验证码)方面的内容,对于java 实现QQ自动登录(带验证码)感 ...
- 运用String类实现一个模拟用户登录程序
package Test; import java.util.Scanner; // 模拟用户登录程序 // 思路: // 1.用两个String类分别接收用户名和密码 // 2.判断输入的用户名和密 ...
- cookielib和urllib2模块相结合模拟网站登录
1.cookielib模块 cookielib模块的主要作用是提供可存储cookie的对象,以便于与urllib2模块配合使用来访问Internet资源.例如可以利用 本模块的CookieJar类的对 ...
- java.net.URL 模拟用户登录网页并维持session
java.net.URL 模拟用户登录网页并维持session 半成品,并非完全有用 import java.io.BufferedReader; import java.io.InputStream ...
- POST模拟百度登录和自动发帖
这里用HttpClient发包模拟百度登录和发帖,验证码部分采用机器下载人工识别. 登陆百度的原理:1. 访问https://passport.baidu.com/v2/api/?getapi& ...
- Java语言使用HttpClient模拟浏览器登录
使用HttpClient来模拟浏览器登录网站,然后可以进行操作,比如发布信息等 第一步:获取实际的post网址,(不考虑复杂情况下) 1.需要使用到firefox的httpfox插件,httpfox中 ...
- python 初学习 模拟用户登录
#!/usr/bin/env python#coding:utf-8''' 2017年8月19日 模拟用户登录,userfile 文件保存字典 用户名,和密码 sorryname 文件保存字典 登录过 ...
- python3.0 模拟用户登录,三次错误锁定
# -*- coding:utf-8 -*- #需求模拟用户登录,超过三次错误锁定不允许登陆 count = 0 #realname passwd Real_Username = &quo ...
- C# httpclient获取cookies实现模拟web登录
目前在公司做一款平台化的产品,我主要负责PC端上的开发,在产品推荐过程中为了节省开发时间很多功能模块没来得及做原生,用CEF嵌入了很多带功能web页面,与客户端进行交互从而实现功能. 在二期开发中,产 ...
- xpath技术解析xml以及案例模拟用户登录效果
问题:当使用dom4j查询比较深的层次结构的节点(标签,属性,文本),比较麻烦!!! xpath就在此情况下产生了--主要是用于快速获取所需的[节点对象]. 在dom4j中如何使用xPath技术 1) ...
随机推荐
- javascript学习笔记之DOM
DOM(文档对象模型),描述了一个层次化的节点树 一.DOM NODE相关公共属性与方法 DOM中所有节点都实现了NODE接口,该接口的公共属性和方法如下: 1.节点基本属性 1)NodeType 节 ...
- 在mac中,npm安装或者卸载失败,提示没有权限
在终端输入 sudo chown -R $USER /usr/local 输入开机密码
- 编译一个需要用特定key前面的应用程序
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) # Build all java files in the java subdirectory L ...
- 客户端浏览器向服务器发起http请求的全过程
http协议的参考:http://blog.csdn.net/hefeng6500/article/details/75081047 (1)浏览器先搜索自身的DNS缓存 (2)操作系统搜索自身的DNS ...
- Dubbo Configuration
可配置参数 http://dubbo.apache.org/zh-cn/docs/user/references/xml/introduction.html 与 spring 整合的几种方式 Spri ...
- SQL Server高可用实现方案
名词解释: WSFC:Windows Server Failover Cluster,在Windows Server操作系统上,由WSFC提供高可用性.故障检测和SQL Server AlwaysOn ...
- 选择company回显appname
function showSupCompany() { var obj = {}; obj.label = ScompanyId.getSelectedLabel(); obj.value = Sco ...
- Slider 滑块
通过拖动滑块在一个固定区间内进行选择 ¶基础用法 在拖动滑块时,显示当前值 通过设置绑定值自定义滑块的初始值 <template> <div class="block&qu ...
- java:LeakFilling(Hibernate)
1.关系型数据库: Oracle / Mysql 数据持久化的技术: IO JDBC XML ... 主流的持久层框架: Hibernate mybatis---->apache产品 JPA( ...
- [译]深入 NGINX: 为性能和扩展所做之设计
来自:http://ifeve.com/inside-nginx-how-we-designed-for-performance-scale/ 这篇文章写了nginx的设计,写的很仔细全面, 同时里面 ...