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 ...
随机推荐
- mysql 启动失败
1 mysql 启动时报:MySQL Daemon failed to start.并且启动失败 2 查看mysql log日志 less /var/log/mysqld.log 3 从两行erro ...
- parse date receiving from mvc jsonresult
if we received data like this: ,"Date":"\/Date(1410969600000)\/", we can parse i ...
- OKhttp初步
OkHttp 使用方法:1.分为同步和异步.同步:1.getRequest request = new Request.Builder().url(utl).build();Response resp ...
- FragmentStatePageradapter 与 FragmentPageradapter的区别
FragmentPageradapter : 会将fragment储存在内存中 每次加载页面读取内存中的fragment FragmentStatePageradapter: 不会将fragment储 ...
- webService访问加密-Soapheader
WebService head加密,可以对 WebService设置访问用户名和密码,增强 WebService的安全性 使 WebService只能被授权用户使用. 具体实现步骤: 1. 定义一个 ...
- Java设计模式(三) 装饰模式
装饰模式:动态的将责任附加到对象上,想要扩展功能,装饰者提供有别于继承的另一种选择. 1,创建顶级类 package com.pattern.decorate; public abstract cla ...
- HTML常用标签与表格标签
超链接标签: <a href="超链接地址" target="_blank">超链接的文字</a> _blank或new是在新网页中打开 ...
- Web前端性能优化教程02:使用内容分发网络
基础知识 服务器离用户越近,HTTP请求的响应时间将更短. CNAME:别名记录,当多个域名需要指向同一服务器IP,可以使用一个域名做A记录指向该服务器IP,然后让多个域名指向该A记录. ICP:In ...
- 实现socket非阻塞设置
刚开始学习,难免有些疏漏之处,请各位多多指教. 程序亲测可以使用.废话不多说. 网络IO模型分为阻塞IO.非阻塞IO.异步IO.IO复用.信号驱动IO. 阻塞IO: 有数据时,直接读:没有数据时,等待 ...
- Oracle触发器实例(网搜)
触发器使用教程和命名规范 目 录触发器使用教程和命名规范 11,触发器简介 12,触发器示例 23,触发器语法和功能 34,例一:行级触发器之一 45,例二:行级触发器之二 46,例三:INSTEA ...