C#中发送以post方式发送http请求,并将参数以json格式传递到body体中
string url="http://........";
ImplantAgentProtectQueryDTO dto = new ImplantAgentProtectQueryDTO();
dto.isProtect = isProtect;
dto.newcode = newcode;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/json";
string strContent = JsonSerializer.Searializer(dto); //序列化为字符串,可以使用自己项目中封装的json方法
using (StreamWriter dataStream = new StreamWriter(request.GetRequestStream()))
{
dataStream.Write(strContent);
dataStream.Close();
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string encoding = response.ContentEncoding;
if (encoding == null || encoding.Length < 1)
{
encoding = "UTF-8"; //默认编码
}
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
retString = reader.ReadToEnd();
return retString;
更多推荐
所有评论(0)