首页 通用示例代码 SendRequest.cs示例代码[C#]
    • SendRequest.cs示例代码

      [C#]
    • C#请求通用函数
    • 作者:Jonathan 阅读数:2343 上传时间:2020-11-21
    	        using System;
                using System.Collections.Generic;
                using System.IO;
                using System.Net;
                using System.Text;
                using System.Web.Script.Serialization;
                
                namespace api_sdk
                {
                 class Program
                 {
                     /**
                      * 转发请求到目的主机
                      * @param method string 请求方法
                      * @param url string 请求地址
                      * @param params Dictionary<string,string> 请求参数
                      * @param headers Dictionary<string,string> 请求头
                      * @return string
                      **/
                     static string api_send_request(string method, string url, Dictionary<string, string> param, Dictionary<string, string> headers)
                     {
                         string result = string.Empty;
                         try
                         {
                             string paramData = "";
                             if (param != null && param.Count > 0)
                             {
                                 StringBuilder sbuilder = new StringBuilder();
                                 foreach (var item in param)
                                 {
                                     if (sbuilder.Length > 0)
                                      {
                                           sbuilder.Append("&");
                                      }
                                      sbuilder.Append(item.Key + "=" + item.Value);
                                  }
                                  paramData = sbuilder.ToString();
                              }
                              method = method.ToUpper();
                              if (method == "GET")
                              {
                                  url = string.Format("{0}?{1}", url, paramData);
                              }
                              HttpWebRequest wbRequest = (HttpWebRequest)WebRequest.Create(url);
                              if (method == "GET")
                              {
                                  wbRequest.Method = "GET";
                              }
                              else if (method == "POST")
                              {
                                  wbRequest.Method = "POST";
                                  wbRequest.ContentType = "application/x-www-form-urlencoded";
                                  wbRequest.ContentLength = Encoding.UTF8.GetByteCount(paramData);
                                  using (Stream requestStream = wbRequest.GetRequestStream())
                                  {
                                      using (StreamWriter swrite = new StreamWriter(requestStream))
                                      {
                                          swrite.Write(paramData);
                                      }
                                  }
                              }
                
                              HttpWebResponse wbResponse = (HttpWebResponse)wbRequest.GetResponse();
                              using (Stream responseStream = wbResponse.GetResponseStream())
                              {
                                  using (StreamReader sread = new StreamReader(responseStream))
                                  {
                                      result = sread.ReadToEnd();
                                  }
                              }
                          }
                          catch
                          {
                              return "";
                          }
                          return result;
                      }
                    }
                }