WebAPI 参数问题

POST:

  1.  public HttpResponseMessage PostTest()

   2. public HttpResponseMessage PostTest(string parame)

   3.public HttpResponseMessage PostTest(object parame)

第一种无参数的请求很简单 正常调用即可

第二种有明确参数类型的请求 尼玛试了一下午都不知道怎么请求

第三种object类型的请求 可以正常调用 但是请求的类型需要指定规范不然传递过去的得到的值是null,建议设置为json传递(ContentType= application/json)

GET:

   1.public HttpResponseMessage GetTest()

   2.public HttpResponseMessage GetTest(string parame)

第一种无参数的请求很简单 正常调用即可

第二种有明确参数类型的请求 直接在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;//接口调用成功获取的数据
            }

        }
原文地址:https://www.cnblogs.com/Rock-Lee/p/10230098.html