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;
}

时间: 2024-10-11 11:51:18

C# Get/Post 模拟提交的相关文章

.Net模拟提交表单

2016-09-0210:49:20 以中邮速递API为服务接口,由于提交方式为表单提交,我要获取返回值来处理其他业务,所以一开始尝试采用Js后台获取返回值,但是涉及到跨域请求限制问题,那边服务端接口返回的是json而不能处理jsonp:所以开始着手考虑后台模拟提交表单: 网上开始查资料,对于后台提交表单的一些相关资料: 表单提交协议规定: 对于含文件的表单提交,要先将 HTTP 要求的 Content-Type 设为 multipart/form-data,而且要设定一个 boundary 参

c# 模拟提交带文件上传的表单

------WebKitFormBoundary8gsfyexbIoO5OYAa Content-Disposition: form-data; name="username" admin ------WebKitFormBoundary8gsfyexbIoO5OYAa Content-Disposition: form-data; name="submit" 登录 ------WebKitFormBoundary8gsfyexbIoO5OYAa Content-D

通过C#的HttpClient模拟提交form()表单

post提交表单一般无非是一般text文本和文件类型,如下 <input type="file"/> <input type="text"/> 如果模拟post提交表单的过程,该怎么做呢 这里就需要用到HttpClietn类 首先我们需要一个类去包装这些需要上载的数据,例如 /// <summary> /// 包装Data数据的Model /// </summary> public class SendData { /

关于libcurl模拟提交form

因为本人对web机制了解较少,在使用C/libcurl中遇到了很多问题.主要的解决方式: 1,在网页源码中找到提交form的定位.也就是网页文件对应form中的action属性.往往都是提交到jsp或者php等脚本中运行的,而不是直接提交到本网页. 2,要注意http头.http头的不同会引起网页的不同反应.我做的一个小项目,在提交到两个网址php脚本时,都因为http头引起php脚本执行不正确. 3,重定向问题. 在一些网页中,返回的网页数据会带有重定向信息,这时要打开curl_easy_se

delphi使用IdHTTP模拟提交页面方法总结

http://blog.csdn.net/lxdcyh/article/details/3986800 1.拖入TIdHTTP控件,HandleRedirect设为True,否则可能会出现HTTP 307错误. 2.主要的设置在Request里.2.1 userAgent应该改为Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; CIBA; MAXTHON 2.

php 使用curl 进行简单模拟提交表单

//初始化curl $ch = curl_init(); $url = 'xxx'; $option = [ CURLOPT_URL => $url, CURLOPT_HEADER => 1, CURLOPT_RETURNTRANSFER => 1, ]; curl_setopt_array($ch, $option); $output = curl_exec($ch); preg_match_all('/Set-Cookie: (.*);/i', $output, $str); //正

php curl模拟post请求提交数据

最近在做校园图书馆图书信息的采集程序,既然是图书馆图书的采集,肯定有提交搜索的页面,无非是post提交,让我想到了curl模拟提交,首先通过firebug进行抓包查询下post提交后的格式如下: txtWxlx=CN&hidWxlx=spanCNLx&txtPY=HZ&txtTm=%D2%F4%C0%D6&txtLx=%25&txtSearchType=1&nMaxCount=100&nSetPageSize=10&cSortFld=%D5%

ASP模拟POST请求异步提交数据的方法

这篇文章主要介绍了ASP模拟POST请求异步提交数据的方法,本文使用MSXML2.SERVERXMLHTTP.3.0实现POST请求,需要的朋友可以参考下 有时需要获取远程网站的某些信息,而服务器又限制了GET方式,只能通过POST数据提交,这个时候我们可以通过asp来实现模拟提交post数据,网上有挺多这样的例子的.下面的是我自己写的比较简洁易懂的函数. 首先,需要一个编码设置的函数,因为asp一般为gbk的,而标准的网站现在大都使用utf-8的.所以需要转换. 代码如下: function

CURL模拟POST提交的二种方法实例

CURL应用广范,本文来介绍CURL模拟POST提交的二种方法实例,他们都是返回json字符串格式. 方法一(返回的是json字符串格式): /** * Curl版本 * 使用方法: * $post_string = "app=request&version=beta"; * request_by_curl('http://facebook.cn/restServer.php',$post_string); */ function actionPost($url,$data){