WebAPI 參數問題

POST:html

  1.  public HttpResponseMessage PostTest()web

   2. public HttpResponseMessage PostTest(string parame)json

   3.public HttpResponseMessage PostTest(object parame)c#

第一種無參數的請求很簡單 正常調用便可服務器

第二種有明確參數類型的請求 尼瑪試了一下午都不知道怎麼請求app

第三種object類型的請求 能夠正常調用 可是請求的類型須要指定規範否則傳遞過去的獲得的值是null,建議設置爲json傳遞(ContentType= application/json)post

GET:編碼

   1.public HttpResponseMessage GetTest()atom

   2.public HttpResponseMessage GetTest(string parame)url

第一種無參數的請求很簡單 正常調用便可

第二種有明確參數類型的請求 直接在URL上加上對應名稱的便可 如   xxxxx?parame=123

c# HttpWebRequest類ContentType值類型列表

  1. text/html : HTML格式
  2.  text/plain :純文本格式
  3.  text/xml : XML格式
  4.  image/gif :gif圖片格式
  5.  image/jpeg :jpg圖片格式
  6.  image/png:png圖片格式

     以application開頭的媒體格式類型:

       application/xhtml+xml :XHTML格式
      application/xml : XML數據格式
      application/atom+xml :Atom XML聚合格式
      application/json : JSON數據格式
      application/pdf :pdf格式
      application/msword : Word文檔格式
      application/octet-stream : 二進制流數據(如常見的文件下載)
      application/x-www-form-urlencoded : <form encType=」」>中默認的encType,form表單數據被編碼爲key/value格式發送到服務器(表單默認的提交數據的格式)

 

 

 

  C#簡單請求

  

        public static string PostWebRequest(string postUrl, string paramData)
        {
            string ret = string.Empty;
            try
            {
                if (!postUrl.StartsWith("http://"))
                    return "";

                byte[] byteArray = Encoding.Default.GetBytes(paramData); //轉化
                HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(postUrl));
                webReq.Method = "POST";
                webReq.ContentType = "application/json";
                                      //application/json

                webReq.ContentLength = byteArray.Length;
                Stream newStream = webReq.GetRequestStream();
                newStream.Write(byteArray, 0, byteArray.Length);//寫入參數
                newStream.Close();
                HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
                StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                ret = sr.ReadToEnd();
                sr.Close();
                response.Close();
                newStream.Close();
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
            return ret;
        }

        public static string PostResponse(string url, string postData)
        {
            if (url.StartsWith("https"))
            {
                System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
            }


            HttpContent httpContent = new StringContent(postData);
            httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            httpContent.Headers.ContentType.CharSet = "utf-8";

            HttpClient httpClient = new HttpClient();
            //httpClient..setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");

            HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result;

            //statusCode = response.StatusCode.ToString();
            if (response.IsSuccessStatusCode)
            {
                string result = response.Content.ReadAsStringAsync().Result;
                return result;
            }

            return null;
        }

        public static string GetResponse(string url, string parame)
        {
            using (var httpClient = new HttpClient())
            {
                if (!string.IsNullOrEmpty(parame))
                {
                    url += "?" + parame;
                }

                var response = httpClient.GetAsync(url).Result;
                var data = response.Content.ReadAsStringAsync().Result;
                return data;//接口調用成功獲取的數據
            }

        }
相關文章
相關標籤/搜索