public static string HttpPost(string url, Dictionary<string, string> parms)
{
try
{
string result = string.Empty;
//设置Http的正文
FormUrlEncodedContent httpContent = new FormUrlEncodedContent(parms);
using (HttpClient httpClient = new HttpClient())
{
//异步Post
HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result;
//输出Http响应状态码
//确保Http响应成功
if (response.IsSuccessStatusCode)
{
//异步读取json
result = response.Content.ReadAsStringAsync().Result;
}
}
return result;
}
catch (HttpRequestException ex)
{
LogManager.WriteError("HttpPost", "token:" + LoginUserInfo.CurrentUser.token + "请求url:" + url);
var msg = "{ code:404 ,data: '',msg: '" + ex.Message + "'}";
return msg;
}
}
//调用实列
string token = this.token;
string url = "http://localhost:80/api";
Dictionary<string, string> dic = new Dictionary<string, string>
{
{ "token", "0f1u2c3k" },
{ "lx", "0" },
{ "data", jsonStr }
};
var handle = APIHelper.HttpPost("http://localhost:8080/api", dic);
|