C# http get与post请求方法

public class HttpTools
{
public static string GetRequest(string url)
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Method = "get";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
string result = sr.ReadToEnd();
return result;
}
} /// <summary>
/// HttpPostByJson
/// </summary>
/// <param name="url"></param>
/// <param name="param"></param>
/// <returns></returns>
public static string HttpPostByJson(string url, string param, WebHeaderCollection headers)
{
string strURL = url;
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)WebRequest.Create(strURL);
//添加信息头(header)
request.Headers = headers;
request.Method = "POST";
request.ContentType = "application/json;charset=UTF-8";
string paraUrlCoded = param;
byte[] payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
request.ContentLength = payload.Length;
Stream writer = request.GetRequestStream();
writer.Write(payload, , payload.Length);
writer.Close();
System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
System.IO.Stream s = response.GetResponseStream();
string StrDate = string.Empty;
StringBuilder sb = new StringBuilder();
StreamReader Reader = new StreamReader(s, Encoding.UTF8);
while ((StrDate = Reader.ReadLine()) != null)
{
sb.Append(StrDate);
}
return sb.ToString();
} /// <summary>
/// PostRequest
/// </summary>
/// <param name="url"></param>
/// <param name="paramList"></param>
/// <returns></returns>
public static string PostRequest(string url, Dictionary<object, object> paramList)
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
StringBuilder sb = new StringBuilder();
foreach (var item in paramList)
{
sb.Append(item.Key + "=" + item.Value + "&");
}
//将参数拼为:"name=test&pwd=123" 这种字符串格式 然后将字符串转为byte数组 最后将byte数组写入请求流中
string paramData = sb.ToString().Trim('&');
byte[] data = System.Text.Encoding.UTF8.GetBytes(paramData);
//设置post方式
request.Method = "post";
//这句不能少 不难post请求 得不到对应的响应结果
request.ContentType = "application/x-www-form-urlencoded";
//设置请求参数的长度
request.ContentLength = data.Length;
Stream stream = request.GetRequestStream();
stream.Write(data, , data.Length);
stream.Close();
/**
* *****************注意事项********************
*
* 不管是get还是post请求最后得到的响应流不能直接stream
* 不难得不到响应结果
*
*
* 直接使用Stream 不能获取响应的结果值
*
* 要使用StreamReader才能获取响应的结果值
*
* Stream stream = response.GetResponseStream();
*
* byte[] data = new byte[2*1024*1024]
*
* int r = stream.Read(data,0,data.Length);
*
* string result = System.Text.Encoding.UTF8.GetString(data, 0, r);
*
*
* **/
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
string result = sr.ReadToEnd();
return result;
}
} /// <summary>
/// 发出GET请求
/// </summary>
/// <param name="Url">请求URL</param>
/// <param name="heades">信息头</param>
/// <returns></returns>
public static string HttpGetByHeader(string Url, WebHeaderCollection heades)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
//添加信息头(header)
request.Headers = heades;
request.Method = "GET";
request.ContentType = "text/html;charset=UTF-8";
using (var response = request.GetResponse())
{
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
string retString = myStreamReader.ReadToEnd();
return retString;
}
}
}

测试代码:

 [HttpGet]
public ActionResult Test()
{
string url = "http://192.168.2.250:8092/Spread/Campaign/GetCampaignId?campaignName=string";
WebHeaderCollection header = new WebHeaderCollection();
header["Token"] = "AF4DAFC2-1E76-4864-AE5B-AE0FB73C64EF";
string result = Common.HttpTools.HttpGetByHeader(url, header);
return Content(result);
} public ActionResult TestPost()
{
string url = "http://192.168.2.250:8092/Spread/Campaign/CreateCampaign";
CampaignArgsModel model = new CampaignArgsModel()
{
CampaignName = "TestPost",
FromEmail = "786483815@qq.com",
From = "From",
Content = "Content",
Interval = ,
Signature = "",
Subject = "Subject",
Schedule = DateTime.Now,
Categories = new List<string>() { "TestPost" }, };
string param = JsonConvert.SerializeObject(model);
WebHeaderCollection header = new WebHeaderCollection();
header["Token"] = "AF4DAFC2-1E76-4864-AE5B-AE0FB73C64EF";
string result = Common.HttpTools.HttpPostByJson(url, param, header);
return Content(result);
}
上一篇:android UI生成器


下一篇:Vue router 的使用--初级