/// <summary>
/// POST请求
/// </summary>
/// <param name="url"></param>
/// <param name="value"></param>
/// <param name="contentType"></param>
/// <param name="mediaType"></param>
/// <returns></returns>
public static string HttpPost(string url, string value, string contentType = "application/json", string mediaType = "json")
{
try
{
string result = "";
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.Timeout = 120000;
httpWebRequest.Method = "POST";
httpWebRequest.ContentType = contentType;
if (!string.IsNullOrEmpty(mediaType))
{
httpWebRequest.MediaType = mediaType;
}
byte[] btBodys = Encoding.UTF8.GetBytes(value);
httpWebRequest.ContentLength = btBodys.Length;
httpWebRequest.GetRequestStream().Write(btBodys, 0, btBodys.Length);
HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream());
result = streamReader.ReadToEnd();
httpWebResponse.Close();
streamReader.Close();
httpWebRequest.Abort();
httpWebResponse.Close();
return result;
}
catch (WebException ex)
{
HttpWebResponse res = (HttpWebResponse)ex.Response;
Stream myResponseStream = res.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
string retString = myStreamReader.ReadToEnd();
}
return "";
}
相关文章
- 02-28C# FTP上传文件时出现"应 PASV 命令的请求,服务器返回了一个与 FTP 连接地址不同的地址。"的错误
- 02-28腾讯云图片鉴黄集成到C# SQL Server 怎么在分页获取数据的同时获取到总记录数 sqlserver 操作数据表语句模板 .NET MVC后台发送post请求 百度api查询多个地址的经纬度的问题 try{}里有一个 return 语句,那么紧跟在这个 try 后的 finally {}里的 code 会 不会被执行,什么时候被执行,在 return 前还是后? js获取某个日期
- 02-28post数据时报错:远程服务器返回错误: (400) 错误的请求。
- 02-28WCF Restful POST JSON数据至Server 始终 报 [远程服务器返回错误: (400) 错误的请求。]
- 02-28C# 有错误日志返回的POST 请求 WebException
- 02-28求助 !WCF Restful POST JSON数据至Server 始终 报 [远程服务器返回错误: (400) 错误的请求。]