参考链接
普通http请求
//通过base64加密:
//string str = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(strJson));
String strURL = "Post请求地址";
System.Net.HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);
request.Method = "POST";
HttpWebRequest.DefaultWebProxy = null;
request.ContentType = "application/json;charset=utf-8";
string data = "{\"key\":\"value\"}";
byte[] param = System.Text.Encoding.UTF8.GetBytes(data);
request.ContentLength = param.Length;
System.IO.Stream writer = request.GetRequestStream();
writer.Write(param, 0, param.Length);
writer.Close();
System.Net.HttpWebResponse response;
try
{
response = (System.Net.HttpWebResponse)request.GetResponse();
}
catch (WebException ex)
{
response = (System.Net.HttpWebResponse)ex.Response;
}
System.IO.StreamReader myreader = new System.IO.StreamReader(response.GetResponseStream(), Encoding.UTF8);
//返回的结果
string strResult = myreader.ReadToEnd();
myreader.Close();
使用RestSharp
string filePath = @"E:\12.png";//上传文件
var client = new RestClient("https://baidu.com");//url接口地址
client.Timeout = -1;
var request = new RestRequest(Method.POST);//提交方法
request.AddHeader("Content-Type", "multipart/form-data");//请求头信息
// request.AddHeader("Content-Type", "application/json");
request.AddFile("media", filePath);//添加文件
request.AddParameter("access_token", "asd");//添加参数
request.AddParameter("type", "image");
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);//返回值
C# Post请求