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 模拟提交的更多相关文章

  1. 模拟提交API数据Pyqt版

    其实这个模拟提交数据之前已经写过篇: Python requests模拟登录 因为现在在做的项目中需要一个debug请求调试API,用PHP的CURL写了一个,又因Pyqt更能直观灵活的显示请求的参数 ...

  2. .Net模拟提交表单

    2016-09-0210:49:20 以中邮速递API为服务接口,由于提交方式为表单提交,我要获取返回值来处理其他业务,所以一开始尝试采用Js后台获取返回值,但是涉及到跨域请求限制问题,那边服务端接口 ...

  3. C# 模拟提交 Form表单的数据

    用 HttpWebRequest Post方法模拟提交Form表单数据时,需要设置 ContentType 为 "application/x-www-form-urlencoded" ...

  4. C# 模拟提交带附件(input type=file)的表单

    今天调用某API时,对于文档中的传入参数:File[] 类型,感觉很陌生,无从下手! 按通常的方式在json参数中加入file的二进制数据提交,一直报错(参数错误)!后来经过多方咨询,是要换一种 表单 ...

  5. 网络爬虫入门(二)模拟提交以及HttpClient修正

    模拟提交就是说我们不自己登陆到客户端,仅仅靠发送请求就模拟了客户端的操作,在现实使用的时候经常用来接收一些需要登录才能获取到的数据,来模拟表单的提交,所以很多时候也被称作虚拟登录,这次的例子是我自己为 ...

  6. c#以POST方式模拟提交表单

    这是我一年前写的一个用C#模拟以POST方式提交表单的代码,现在记录在下面,以免忘记咯.那时候刚学C#~忽忽..很生疏..代码看上去也很幼稚 臃肿不堪 #region 内容添加函数(Contentin ...

  7. ASP.NET HTTP模拟提交通用类 GET POST

     用法: WebRequestSugar ws = new WebRequestSugar(); //可选参数 //ws.SetAccept //ws.SetContentType //ws.SetC ...

  8. jQuery 方式模拟提交表单

    //add test moudle define(function(require , exports , module) { //=========== 不使用模块化只使用如下代码即可 start ...

  9. C#.net模拟提交表单POST

    方法一.System.Net.WebClient WebClientObj        = new System.Net.WebClient();   System.Collections.Spec ...

  10. PHP CURL模拟提交数据 攻击N次方

    public function actionCurl(){ $data['DATA']='{"NAME":"c","LEGEND":&quo ...

随机推荐

  1. md5加密31位

    今天将其它服务器里的用户数据导入到新的系统数据库中 出现密码不匹配情况 查看原来数据库中密码得到结果位: 原服务器密码 明文 正确32位密闻 67b14728ad9902aecba32e22fa4f6 ...

  2. 35-less 简明笔记

    分屏显示文本文件 less [options] [file-list] less与more类似,但比more更加完善 例如:在显示一屏文本之后,less将显示提示副等待下一条命令的输入;可以向前或向后 ...

  3. 动态sql语句输出参数

    ) declare @cou int ) ' set @sql='select @count=count(*) from emp where id=@id' exec sp_executesql @s ...

  4. CSS3自动添加省略号

    text-overflow:ellipsis; white-space:nowrap; overflow:hidden; 不换行,一行显示溢出时,文本自动换行.以前都是js计算的,现在可好. elli ...

  5. 轻松搞定C语言中复杂的声明

    参考资料: [1] <C专家编程> 第3章 分析C语言的声明,p64-66

  6. 【bzoj2654】 tree

    http://www.lydsy.com/JudgeOnline/problem.php?id=2654 (题目链接) 题意 给你一个无向带权连通图,每条边是黑色或白色.让你求一棵最小权的恰好有nee ...

  7. CSS3伪类选择器:nth-child()(nth-child(odd)/nth-child(even))

    nth-child(odd):奇数 nth-child(even):偶数 使用时,如果是精确到数字时,切记是从同一级别的元素开始计算,而不是指定某个类才开始计算. 比如: <li>< ...

  8. Windows Server 2012/2016在桌面上添加计算机等图标

    [CMD]->输入[rundll32.exe shell32.dll,Control_RunDLL desk.cpl,,0],回车:

  9. Bzoj1497 [NOI2006]最大获利

    Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 4449  Solved: 2181 Description 新的技术正冲击着手机通讯市场,对于各大运营商来 ...

  10. [NOIP2013] 提高组 洛谷P1969 积木大赛

    题目描述 春春幼儿园举办了一年一度的“积木大赛”.今年比赛的内容是搭建一座宽度为n的大厦,大厦可以看成由n块宽度为1的积木组成,第i块积木的最终高度需要是hi. 在搭建开始之前,没有任何积木(可以看成 ...