首页 > 其他 > 详细

post 请求数据返回 Unsupported Media Type

时间:2017-10-10 12:26:49      阅读:414      评论:0      收藏:0      [点我收藏+]

出现这个“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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!