- 樓主 / webdriver
- 時間: 2014-9-24 11:15Only if you know what's the meaning ... google is your friend
代碼:
public static string HTTP_POST(string Url, string Data, string Cookie, string ContentType)
{
string Out = String.Empty;
System.Net.HttpWebRequest req = System.Net.HttpWebRequest.Create(Url) as System.Net.HttpWebRequest;
try
{
req.Method = "POST";
req.Timeout = 100000;
req.ContentType = ContentType; //something like "multipart/form-data; boundary= xxxxxxxxxxx";
req.UserAgent = "Safari/537.36";
req.CookieContainer = new CookieContainer();
req.CookieContainer.Add(new Uri("http://www.google.com"), GetCookieCollectionFromCookieString(Cookie)); // <-- replace the Uri of your own target
req.Headers.Add("Accept-Encoding", "gzip,deflate");
req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
req.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
byte[] sentData = Encoding.UTF8.GetBytes(Data); // <-- change your encoding if you want to publish different content
req.ContentLength = sentData.Length;
using (System.IO.Stream sendStream = req.GetRequestStream())
{
sendStream.Write(sentData, 0, sentData.Length);
sendStream.Close();
}
System.Net.WebResponse res = req.GetResponse();
System.IO.Stream ReceiveStream = res.GetResponseStream();
using (System.IO.StreamReader sr = new System.IO.StreamReader(ReceiveStream, Encoding.UTF8)) //<-- change encoding if you know the website's encoding method
{
Char[] read = new Char[256];
int count = sr.Read(read, 0, 256);
while (count > 0)
{
String str = new String(read, 0, count);
Out += str;
count = sr.Read(read, 0, 256);
}
}
}
catch (ArgumentException ex)
{
Out = string.Format("HTTP_ERROR :: The second HttpWebRequest object has raised an Argument Exception as 'Connection' Property is set to 'Close' :: {0}", ex.Message);
}
catch (WebException ex)
{
Out = string.Format("HTTP_ERROR :: WebException raised! :: {0}", ex.Message);
}
catch (Exception ex)
{
Out = string.Format("HTTP_ERROR :: Exception raised! :: {0}", ex.Message);
}
return Out;
}
private static CookieCollection GetCookieCollectionFromCookieString(string Cookie)
{
CookieCollection cl = new CookieCollection();
string[] pp = Cookie.Split('=',';');
for(int i = 0; i<pp.Length;)
{
Cookie ck = new Cookie(pp[i].Trim(), pp[i + 1].Trim());
cl.Add(ck);
i += 2;
}
return cl;
}
-
- 第 2 樓 / webdriver
- 時間: 2014-9-24 11:16
-
- 第 3 樓 / callmelxylt
- 時間: 2014-9-24 11:18
webdriver 寫道: Only if you know what's the meaning ... google is your friend
代碼:
public static string HTTP_POST(string Url, string Data, string Cookie, string ContentType)
{
string Out = String.Empty;
System.Net.HttpWebRequest req = System.Net.HttpWebRequest.Create(Url) as System.Net.HttpWebRequest;
try
{
req.Method = "POST";
req.Timeout = 100000;
req.ContentType = ContentType; //something like "multipart/form-data; boundary= xxxxxxxxxxx";
req.UserAgent = "Safari/537.36";
req.CookieContainer = new CookieContainer();
req.CookieContainer.Add(new Uri("http://www.google.com"), GetCookieCollectionFromCookieString(Cookie));
byte[] sentData = Encoding.UTF8.GetBytes(Data);
req.ContentLength = sentData.Length;
using (System.IO.Stream sendStream = req.GetRequestStream())
{
sendStream.Write(sentData, 0, sentData.Length);
sendStream.Close();
}
System.Net.WebResponse res = req.GetResponse();
System.IO.Stream ReceiveStream = res.GetResponseStream();
using (System.IO.StreamReader sr = new System.IO.StreamReader(ReceiveStream, Encoding.UTF8))
{
Char[] read = new Char[256];
int count = sr.Read(read, 0, 256);
while (count > 0)
{
String str = new String(read, 0, count);
Out += str;
count = sr.Read(read, 0, 256);
}
}
}
catch (ArgumentException ex)
{
Out = string.Format("HTTP_ERROR :: The second HttpWebRequest object has raised an Argument Exception as 'Connection' Property is set to 'Close' :: {0}", ex.Message);
}
catch (WebException ex)
{
Out = string.Format("HTTP_ERROR :: WebException raised! :: {0}", ex.Message);
}
catch (Exception ex)
{
Out = string.Format("HTTP_ERROR :: Exception raised! :: {0}", ex.Message);
}
return Out;
}
private static CookieCollection GetCookieCollectionFromCookieString(string Cookie)
{
CookieCollection cl = new CookieCollection();
string[] pp = Cookie.Split('=',';');
for(int i = 0; i<pp.Length;)
{
Cookie ck = new Cookie(pp[i].Trim(), pp[i + 1].Trim());
cl.Add(ck);
i += 2;
}
return cl;
}
Thanks for sharing - 第 4 樓 / 飛舞的音符
- 時間: 2014-9-24 11:18
- 第 5 樓 / webdriver
- 時間: 2014-9-24 11:19
- 第 6 樓 / 小閃
- 時間: 2014-9-24 11:19
- 第 7 樓 / callmelxylt
- 時間: 2014-9-24 11:19
- 第 8 樓 / webdriver
- 時間: 2014-9-24 11:20
- 第 9 樓 / 飛舞的音符
- 時間: 2014-9-24 11:20
- 第 10 樓 / callmelxylt
- 時間: 2014-9-24 11:20