對http請求進行過濾處理,轉換成接收著需要的格式

來源:互聯網
上載者:User

標籤:str   list   formdata   add   請求   json   context   http請求   headers   

 
需要在Global.asax的Application中進行初始化處理 這樣:GlobalConfiguration.Configuration.MessageHandlers.Add(new DefaultHandler())
然後在App_Start檔案夾中添加 DefaultHandler 類複製下面的代碼
public class DefaultHandler : DelegatingHandler{protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken){request.RequestUri = new Uri(request.RequestUri.ToString());MediaTypeHeaderValue contentType = request.Content.Headers.ContentType;if (contentType != null){switch (contentType.MediaType){case "application/x-www-form-urlencoded":{NameValueCollection formData = await request.Content.ReadAsFormDataAsync(cancellationToken);request.Content = new FormUrlEncodedContent(Correct(formData));//如下代碼為了預下訂單使用var formDataDic = formData.ToDictionary();var str = ConvertToJsonString(formDataDic);HttpContext.Current.Request.Headers.Add("formDataToJson", str);}break;case "multipart/form-data":{NameValueCollection formData = await request.Content.ReadAsFormDataAsync(cancellationToken);request.Content = new FormUrlEncodedContent(Correct(formData));}break;case "application/json":{HttpContentHeaders oldHeaders = request.Content.Headers;string formData = await request.Content.ReadAsStringAsync();request.Content = new StringContent(formData);ReplaceHeaders(request.Content.Headers, oldHeaders);}break;default:throw new Exception("Implement It!");}}return await base.SendAsync(request, cancellationToken);}private static IEnumerable<KeyValuePair<string, string>> Correct(NameValueCollection formData){return formData.Keys.Cast<string>().Select(key => new KeyValuePair<string, string>(key, formData[key])).ToList();}private static void ReplaceHeaders(HttpHeaders currentHeaders, HttpHeaders oldHeaders){currentHeaders.Clear();foreach (var item in oldHeaders)currentHeaders.Add(item.Key, item.Value);}private string ConvertToJsonString(IDictionary<string, string> param){dynamic myObj = new ExpandoObject();var dic = (IDictionary<string, object>)myObj;foreach (var item in param){if (item.Value.StartsWith("[") || item.Value.StartsWith("{")){dic[item.Key] = JsonConvert.DeserializeObject(item.Value);}else{dic[item.Key] = item.Value;}}return JsonConvert.SerializeObject(myObj);}}public static class NVCExtender{public static IDictionary<string, string> ToDictionary(this NameValueCollection source){return source.AllKeys.ToDictionary(k => k, k => source[k]);}}

 

對http請求進行過濾處理,轉換成接收著需要的格式

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.