Forked from Jia-Hong-Peng/gist:f559a885daaa4dbd318fe4798ad93e3a
Created
April 19, 2018 08:43
-
-
Save sd016808/f5a1cc0c476168dd89bd68abc05b7711 to your computer and use it in GitHub Desktop.
http post get
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// 取得必要的post參數 | |
/// </summary> | |
/// <returns></returns> | |
public static string getEVENTVALIDATION() | |
{ | |
string output = htmls; | |
//抓取值 | |
output = GetInner(output, "__EVENTVALIDATION", "<table width=\"939\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" bgcolor=\"fff299\">"); | |
output = GetInner(output, "value=\"", "\" />"); | |
output = output.Replace("value=\"", ""); | |
return output; | |
} | |
/// <summary> | |
/// 取得必要的post參數 | |
/// </summary> | |
/// <returns></returns> | |
public static string getVIEWSTATE() | |
{ | |
string output = htmls; | |
//抓取值 | |
output = GetInner(output, "__VIEWSTATE", "__VIEWSTATEENCRYPTED"); | |
output = GetInner(output, "value=\"", "\" />"); | |
output = output.Replace("value=\"", ""); | |
return output; | |
} | |
/// <summary> | |
/// 取得中間值 | |
/// </summary> | |
/// <param name="htmls">內文</param> | |
/// <param name="ff">頭</param> | |
/// <param name="ll">尾</param> | |
/// <returns></returns> | |
public static string GetInner(string htmls, string ff, string ll) | |
{ | |
int first = htmls.IndexOf(ff); //關鍵字1 | |
int last = htmls.LastIndexOf(ll); //關鍵字2 | |
try | |
{ | |
string output = htmls.Substring(first, last - first); | |
return output; | |
} | |
catch (Exception e) | |
{ | |
return e.ToString(); | |
} | |
} | |
//接收網頁數據 | |
private static string httpGet(string URI) | |
{ | |
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(URI); | |
//req.Proxy = new WebProxy(ProxyString, true); //true means no proxy | |
WebResponse resp = req.GetResponse(); | |
StreamReader sr = new StreamReader(resp.GetResponseStream(), Encoding.GetEncoding("UTF-8")); | |
string sReturn = sr.ReadToEnd().Trim(); | |
resp.Close(); sr.Close(); | |
return sReturn; | |
} | |
//向網頁提交數據並接收返回信息: | |
private static string httpPost(string URI, string Parameters) | |
{ | |
byte[] bytes = Encoding.Default.GetBytes(Parameters); | |
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(URI); | |
req.CookieContainer = new CookieContainer(); | |
// req.Proxy = new WebProxy(ProxyString, true); | |
//req.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-TW; rv:1.9) Gecko/2008052906 Firefox/3.0"; | |
//req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; | |
req.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.8) Gecko/20051111 Firefox/1.5"; | |
req.Accept = "text/xml,application/xml,application/xhtml+xml,text/html"; | |
req.Timeout = 10000; | |
req.KeepAlive = true; | |
req.Referer = URI; //修改為自己的referer | |
req.ContentType = "application/x-www-form-urlencoded"; | |
req.Method = "POST"; | |
req.ContentLength = bytes.Length; | |
Stream os = req.GetRequestStream(); | |
os.Write(bytes, 0, bytes.Length); //以上向伺服器post信息。 | |
os.Close(); | |
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();//以下獲取伺服器返回信息 | |
if (resp == null) return null; | |
StreamReader sr = new StreamReader(resp.GetResponseStream(), Encoding.GetEncoding("UTF-8")); | |
string sReturn = sr.ReadToEnd().Trim(); | |
resp.Close(); sr.Close(); | |
return sReturn; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment