C# Get/Post 模拟提交
public static string GetPage(string url, string encoding)
{
PublicVariables.NetworkConnection = false;
int browserTimeout = PublicVariables.BrowserTimeout;
string str = PublicVariables.Cfg_Agent;
string str2 = PublicVariables.last_url;
CookieContainer cookie = new CookieContainer();
string str3 = "";
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(url));
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "GET";
request.CookieContainer = cookie;
request.Referer = str2;
request.Timeout = browserTimeout; request.ReadWriteTimeout = browserTimeout;
request.AllowAutoRedirect = true;
request.UserAgent = str;
request.Proxy = null;
if (url.StartsWith("https://") && url.Contains(".taobao.com"))
{
request.ProtocolVersion = HttpVersion.Version10;
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (encoding == "")
str3 = new StreamReader(response.GetResponseStream(), Encoding.Default).ReadToEnd();
else
str3 = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding)).ReadToEnd();
str2 = url;
PublicVariables.NetworkConnection = true;
return str3;
}
catch (Exception exception)
{
PublicVariables.NetworkConnection = false;
return exception.ToString();
}
}
public static string GetPage(string url,string last_url, string encoding,ref CookieContainer cookie )
{
//PublicVariables.NetworkConnection = false;
int browserTimeout = PublicVariables.BrowserTimeout*5;
string str = PublicVariables.Cfg_Agent;
string str2 = last_url;
string str3 = "";
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(url));
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "GET";
request.CookieContainer = cookie ;
request.Referer = str2;
request.Timeout = browserTimeout;
request.ReadWriteTimeout = browserTimeout;
request.AllowAutoRedirect = true;
request.UserAgent = str;
request.Proxy = null;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (encoding == "")
str3 = new StreamReader(response.GetResponseStream()).ReadToEnd();
else
str3 = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding)).ReadToEnd();
str2 = url;
if (response.Headers == null) { }
else
{
for (int jj = 0; jj < response.Headers.Count; jj++)
{
string aa = response.Headers[jj].ToString();
}
}
if (response.Cookies == null)
{ }
else
{
for (int jj = 0; jj < response.Cookies.Count; jj++)
{
Cookie ccc = response.Cookies[jj];
string key, value;
key = ccc.Name;
value = ccc.Value;
}
}
cookie.Add(response.Cookies);
return str3;
}
catch (Exception exception)
{
return exception.ToString();
}
}
public static string GetPage(string url, string last_url, string encoding, ref CookieContainer cookie,ref HttpWebResponse rsp)
{
int browserTimeout = PublicVariables.BrowserTimeout ;
string str = PublicVariables.Cfg_Agent;
string str2 = last_url;
string str3 = "";
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(url));
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "GET";
request.CookieContainer =cookie ;
request.Referer = str2;
request.Timeout = browserTimeout * 2;
request.ReadWriteTimeout = browserTimeout*2;
request.AllowAutoRedirect = true;
request.UserAgent = str;
request.Proxy = null;
if (url.StartsWith("https://"))
{
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (encoding == "")
str3 = new StreamReader(response.GetResponseStream()).ReadToEnd();
else
str3 = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding)).ReadToEnd();
str2 = url;
if (response.Headers == null) { }
else
{
for (int jj = 0; jj < response.Headers.Count; jj++)
{
string aa = response.Headers[jj].ToString();
}
}
if (response.Cookies == null)
{ }
else
{
for (int jj = 0; jj < response.Cookies.Count; jj++)
{
Cookie ccc = response.Cookies[jj];
string key, value;
key = ccc.Name;
value = ccc.Value;
}
}
cookie.Add(response.Cookies);
rsp = response;
return str3;
}
catch (Exception exception)
{
return exception.ToString();
}
}
public static string GetPageToFile(string url,string last_url, string path, ref CookieContainer cookie)
{
int browserTimeout = PublicVariables.BrowserTimeout ;
string str2 = PublicVariables.Cfg_Agent;
string str3 = last_url;
//CookieContainer cookie = PublicVariables.cookie;
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(url));
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "GET";
request.CookieContainer = cookie;
request.Referer = str3;
request.Timeout = browserTimeout;
request.AllowAutoRedirect = true; request.Proxy = null; request.ServicePoint.Expect100Continue = false;
request.UserAgent = str2; request.ServicePoint.Expect100Continue = false;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
SaveBinaryFile(response, path);
str3 = url;
cookie.Add(response.Cookies);
return path;
}
catch
{
return "";
}
return "";
}
public static string PostPage(string url, string last_url, Hashtable PostCs, string encoding, ref CookieContainer cookie)
{
PublicVariables.NetworkConnection = false;
string s = "";
string str2 = "";
int browserTimeout = PublicVariables.BrowserTimeout ;
string str3 = PublicVariables.Cfg_Agent;
string str4 = last_url;
ArrayList list = new ArrayList(PostCs.Keys);
foreach (string str5 in list)
{
s = s + "&" + str5 + "=" + PostCs[str5].ToString();
}
if (s.Length > 0)
{
s = s.Remove(0, 1);
}
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(url));
byte[] bytes = Encoding.UTF8.GetBytes(s);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = bytes.Length;
request.Method = "POST";
request.CookieContainer = cookie;
request.Referer = str4;
request.Timeout = browserTimeout;
request.ReadWriteTimeout = browserTimeout;
request.AllowAutoRedirect = true;
request.Proxy = null;
request.ServicePoint.Expect100Continue = false;
request.UserAgent = str3;
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.2; WOW64; Trident/6.0; .NET4.0E; .NET4.0C; .NET CLR 3.5.30729; .NET CLR 2.0.50727; .NET CLR 3.0.30729; Tablet PC 2.0)";
request.Accept = "application/x-shockwave-flash, image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
if (url.StartsWith("https://"))
{
X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);
X509Certificate2 cert;
int cc = store.Certificates.Count;
request.ClientCertificates = store.Certificates;
for (int iii = 0; iii < store.Certificates.Count; iii++)
{
cert = store.Certificates[iii];
//cert.IssuerName.Name
}
}
Stream requestStream = request.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Flush();
requestStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader;
if (encoding == "")
reader = new StreamReader(responseStream);
else
reader = new StreamReader(responseStream, Encoding.GetEncoding(encoding));
str2 = reader.ReadToEnd();
reader.Close();
responseStream.Close();
str4 = url;
cookie.Add(response.Cookies);
IEnumerator enumerator=null ;
string str = "";
try
{
enumerator = cookie.GetCookies(new Uri(url)).GetEnumerator();
while (enumerator.MoveNext())
{
str = str +System.Runtime.CompilerServices. RuntimeHelpers.GetObjectValue(enumerator.Current).ToString() + "\r\n";
}
}
finally
{
if (enumerator is IDisposable)
{
(enumerator as IDisposable).Dispose();
}
}
return str2;
}
catch (Exception exception)
{
return exception.ToString();
}
}
public static string PostPage(string url, string last_url, string data, string encoding, ref CookieContainer cookie)
{
PublicVariables.NetworkConnection = false;
string s = "";
string str2 = "";
int browserTimeout = PublicVariables.BrowserTimeout;
string str3 = PublicVariables.Cfg_Agent;
string str4 = last_url;
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(url));
byte[] bytes = Encoding.UTF8.GetBytes(data);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = bytes.Length;
request.Method = "POST";
request.CookieContainer = cookie;
request.Referer = str4;
request.Timeout = browserTimeout;
request.ReadWriteTimeout = browserTimeout;
request.AllowAutoRedirect = true;
request.Proxy = null;
request.ServicePoint.Expect100Continue = false;
request.UserAgent = str3;
request.UserAgent = str3;// "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.2; WOW64; Trident/6.0; .NET4.0E; .NET4.0C; .NET CLR 3.5.30729; .NET CLR 2.0.50727; .NET CLR 3.0.30729; Tablet PC 2.0)";
request.Accept = "application/x-shockwave-flash, image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
if (url.StartsWith("https://"))
{
X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);
X509Certificate2 cert;
int cc = store.Certificates.Count;
request.ClientCertificates = store.Certificates;
for (int iii = 0; iii < store.Certificates.Count; iii++)
{
cert = store.Certificates[iii];
//cert.IssuerName.Name
}
}
Stream requestStream = request.GetRequestStream();
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Flush();
requestStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader;
if (encoding == "")
reader = new StreamReader(responseStream);
else
reader = new StreamReader(responseStream, Encoding.GetEncoding(encoding));
str2 = reader.ReadToEnd();
reader.Close();
responseStream.Close();
str4 = url;
cookie.Add(response.Cookies);
IEnumerator enumerator = null;
string str = "";
try
{
enumerator = cookie.GetCookies(new Uri(url)).GetEnumerator();
while (enumerator.MoveNext())
{
str = str + System.Runtime.CompilerServices.RuntimeHelpers.GetObjectValue(enumerator.Current).ToString() + "\r\n";
}
}
finally
{
if (enumerator is IDisposable)
{
(enumerator as IDisposable).Dispose();
}
}
return str2;
}
catch (Exception exception)
{
return exception.ToString();
}
}
private static bool SaveBinaryFile(WebResponse response, string FileName)
{
bool flag = true;
byte[] buffer = new byte[0x400];
try
{
int num;
if (System.IO.File.Exists(FileName))
{
System.IO.File.Delete(FileName);
}
Stream stream = System.IO.File.Create(FileName);
Stream responseStream = response.GetResponseStream();
do
{
num = responseStream.Read(buffer, 0, buffer.Length);
if (num > 0)
{
stream.Write(buffer, 0, num);
}
}
while (num > 0);
stream.Close();
responseStream.Close();
}
catch
{
flag = false;
}
return flag;
}
C# Get/Post 模拟提交的更多相关文章
- 模拟提交API数据Pyqt版
其实这个模拟提交数据之前已经写过篇: Python requests模拟登录 因为现在在做的项目中需要一个debug请求调试API,用PHP的CURL写了一个,又因Pyqt更能直观灵活的显示请求的参数 ...
- .Net模拟提交表单
2016-09-0210:49:20 以中邮速递API为服务接口,由于提交方式为表单提交,我要获取返回值来处理其他业务,所以一开始尝试采用Js后台获取返回值,但是涉及到跨域请求限制问题,那边服务端接口 ...
- C# 模拟提交 Form表单的数据
用 HttpWebRequest Post方法模拟提交Form表单数据时,需要设置 ContentType 为 "application/x-www-form-urlencoded" ...
- C# 模拟提交带附件(input type=file)的表单
今天调用某API时,对于文档中的传入参数:File[] 类型,感觉很陌生,无从下手! 按通常的方式在json参数中加入file的二进制数据提交,一直报错(参数错误)!后来经过多方咨询,是要换一种 表单 ...
- 网络爬虫入门(二)模拟提交以及HttpClient修正
模拟提交就是说我们不自己登陆到客户端,仅仅靠发送请求就模拟了客户端的操作,在现实使用的时候经常用来接收一些需要登录才能获取到的数据,来模拟表单的提交,所以很多时候也被称作虚拟登录,这次的例子是我自己为 ...
- c#以POST方式模拟提交表单
这是我一年前写的一个用C#模拟以POST方式提交表单的代码,现在记录在下面,以免忘记咯.那时候刚学C#~忽忽..很生疏..代码看上去也很幼稚 臃肿不堪 #region 内容添加函数(Contentin ...
- ASP.NET HTTP模拟提交通用类 GET POST
用法: WebRequestSugar ws = new WebRequestSugar(); //可选参数 //ws.SetAccept //ws.SetContentType //ws.SetC ...
- jQuery 方式模拟提交表单
//add test moudle define(function(require , exports , module) { //=========== 不使用模块化只使用如下代码即可 start ...
- C#.net模拟提交表单POST
方法一.System.Net.WebClient WebClientObj = new System.Net.WebClient(); System.Collections.Spec ...
- PHP CURL模拟提交数据 攻击N次方
public function actionCurl(){ $data['DATA']='{"NAME":"c","LEGEND":&quo ...
随机推荐
- jQuery插件---exselect实现联动
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title> ...
- php上传文件
php部分: <?php /* 注释:允许用户上传文件是一个巨大的安全风险. 请仅仅允许可信的用户执行文件上传操作. */ header("Content-type: text/htm ...
- java中的@Override标签,小细节大作用
转载:http://www.cnblogs.com/octobershiner/archive/2012/03/09/2388370.html 一般用途 ...
- Java--剑指offer(10)
46.每年六一儿童节,牛客都会准备一些小礼物去看望孤儿院的小朋友,今年亦是如此.HF作为牛客的资深元老,自然也准备了一些小游戏.其中,有个游戏是这样的:首先,让小朋友们围成一个大圈.然后,他随机指定一 ...
- [转]理解JavaScript中的事件处理
什么是事件? 事件(Event)是JavaScript应用跳动的心脏 ,也是把所有东西粘在一起的胶水.当我们与浏览器中 Web 页面进行某些类型的交互时,事件就发生了.事件可能是用户在某些内容上的点击 ...
- 控件(文本类): TextBox, PasswordBox
介绍背水一战 Windows 10 之 控件(文本类) TextBox PasswordBox 示例1.TextBox 的示例 1Controls/TextControl/TextBoxDemo1.x ...
- Get it,你离几何达人不远了!
对于爱学几何的人,是否存在这样的困扰:没有标准的尺规工具,图形画的不标准,理解上总是出错......整天在纸上画图,浪费大把大把的时间......几何图形画的不美观,在别人面前都拿不出手,公开课上都没 ...
- iframe与主框架跨域相互访问方法【转】
转自:http://blog.csdn.net/fdipzone/article/details/17619673 1.同域相互访问 假设A.html 与 b.html domain都是localho ...
- 【USACO 1.4】Mother's Milk
/* TASK: milk3 LANG: C++ SOLVE: 倒水,dfs,枚举每一种倒法,ca[i][j]记录a和c桶的状态,因为总体积不变,故b的状态不需要记录. */ #include< ...
- adb错误解决
1.adb是什么?ADB全称Android Debug Bridge, 是android sdk里的一个工具,用这个工具可以直接操作管理android模拟器或者真实的andriod设备. 2.调试安卓 ...