using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using System.Web;
namespace Test
{
/// <summary>
/// HTTP From 表单模拟请求工具类
/// </summary>
public class HttpFormClient
{
public static string SendToWebByHttpClient<T>(string url, T value)
{
var modelType = typeof(T);
using (var client = new HttpClient())
using (var formData = new MultipartFormDataContent())
{
//遍历SendData的所有成员
foreach (var item in modelType.GetProperties())
{
HttpContent content;
content = new StringContent(((string)item.GetValue(value).ToString()));
formData.Add(content, item.Name);
}
HttpResponseMessage response = client.PostAsync(url, formData).Result;
//请求成功
if (response.IsSuccessStatusCode) {
var res = response.Content.ReadAsStringAsync().Result;
return res;
}
return "";
}
}
}
}
模拟表单进行提交,请求头格式为 Content-Type:application/x-www-form-urlencoded
及接收响应的返回值