码迷,mamicode.com
首页 > 其他好文 > 详细

post 请求数据返回 Unsupported Media Type

时间:2017-10-10 13:09:39      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:ring   encoding   coding   ati   string   ica   toe   charset   get   

出现这个“415 Unsupported Media Type” 错误的原因:

经过自己的测试,最终是没有加上“webrequest.ContentType = "application/json;charset=UTF-8";”  这句话规定请求的数据为json所导致的。

请求post的代码如下:

public static string GetPostData(string url, string paramstr)
        {
            HttpWebRequest webrequest = (HttpWebRequest)HttpWebRequest.Create(url);
            webrequest.Method = "post";
            webrequest.ContentType = "application/json;charset=UTF-8";

            byte[] postdatabyte = Encoding.UTF8.GetBytes(paramstr);
            webrequest.ContentLength = postdatabyte.Length;
            Stream stream;
            stream = webrequest.GetRequestStream();
            stream.Write(postdatabyte, 0, postdatabyte.Length);
            stream.Close();



            using (var httpWebResponse = webrequest.GetResponse())
            using (StreamReader responseStream = new StreamReader(httpWebResponse.GetResponseStream()))
            {

                String ret = responseStream.ReadToEnd();



                return ret;
            }
        }

这样就会正常返回请求的json数据了。

post 请求数据返回 Unsupported Media Type

标签:ring   encoding   coding   ati   string   ica   toe   charset   get   

原文地址:http://www.cnblogs.com/flytosky-xy/p/7644545.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!