/// <summary> 创建Get请求对象 </summary> /// <param name="param"></param> /// <returns></returns> private static HttpWebRequest createHttpWebGetRequest(HttpTaskParams param) { if (param.getStringParams() == null) { return((HttpWebRequest)HttpWebRequest.Create(param.Url)); } StringBuilder urlSb = new StringBuilder(); urlSb.Append(param.Url); if (param.HasStringParams()) { if (param.Url.Contains("?")) { urlSb.Append('?'); } if (param.Url.Contains("&")) { urlSb.Append("&"); } } urlSb.Append(GetStringByPairs(param.getStringParams(), param.IsAutoEncoding())); HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(urlSb.ToString()); return(request); }
/// <summary> 设置请求体 </summary> /// <param name="request"></param> /// <param name="param"> </param> public static void SetHttpWebRequestBodyContent(HttpWebRequest request, HttpTaskParams param) { if (request == null || param == null) { return; } if (param.Method == HttpTaskParams.METHOD_GET) { //nothing,get 请求没有请求体,也不允许写请求体? } else { //POST 或 其他类型,优先写自定义请求体 if (string.IsNullOrEmpty(param.CustomStringContentBody)) { if (param.getFileParams() == null) { //写post请求体 SetHttpWebRequestStringBodyContent(request, GetStringByPairs(param.getStringParams(), param.IsAutoEncoding())); } else { //写上传请求体 SetHttpWebRequestUploadBodyContent(request, param); } } else { //写自定义请求体 SetHttpWebRequestStringBodyContent(request, param.CustomStringContentBody); } } }