//Post請求
public static string Post(string url,string obj=null)
{
string param = (obj);//參數
byte[] bs = Encoding.Default.GetBytes(param);
//創建一個新的HttpWebRequest對象。
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
// 將方法屬性設置為“POST”以將數據發布到URI。
req.Method = "POST";
//設置contentType屬性。
req.ContentType = "application/json";
req.ContentLength = bs.Length;
using (Stream reqStream = req.GetRequestStream())
{
reqStream.Write(bs, 0, bs.Length);
reqStream.Close();
HttpWebResponse response2 = (HttpWebResponse)req.GetResponse();
StreamReader sr2=new StreamReader(response2.GetResponseStream(), Encoding.UTF8);
string text2 = sr2.ReadToEnd();
return text2;
}
}
//Get請求
public static string Get(string func, string strParam = null)
{
string result = "";
StringBuilder realUrl = new StringBuilder();
realUrl.Append(func).Append("?");
StringBuilder param = new StringBuilder();
if (strParam != null)
{
if (param.Length > 0)
{
param.Append("&");
}
param.Append(strParam);
}
realUrl.Append(param.ToString());
WebRequest req = WebRequest.Create(realUrl.ToString());
req.ContentType = "text/html; charset=GBK";
WebResponse res = req.GetResponse();
Stream stream = res.GetResponseStream();
using (StreamReader reader = new StreamReader(stream, Encoding.GetEncoding("gb2312")))
{
result = reader.ReadToEnd();
}
return result;
}
//Get請求
public static string Get(string func)
{
using (System.IO.StreamReader reader = new System.IO.StreamReader(WebRequest.Create(func).GetResponse().GetResponseStream(), System.Text.Encoding.GetEncoding("utf-8")))
{
return reader.ReadToEnd();
}
}