HttpWebRequest发送json数据返回400

handhappy 2017-08-30 10:33:05
HttpWebRequest发送json数据返回400,但是用post接口测试工具测试返回正常数据
...全文
700 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
handhappy 2017-08-31
  • 打赏
  • 举报
回复
问题解决了,是传入的json参数格式错误
handhappy 2017-08-30
  • 打赏
  • 举报
回复
/// <summary>
        /// 发送http请求
        /// </summary>
        /// <param name="method">请求模式 POST、GET</param>
        /// <param name="url">url</param>
        /// <param name="param">参数 </param>
        /// <returns></returns>
        public static string SendHttpRequest(string method, string url, string param)
        {
            try
            {
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);

                request.Method = method;  //设置请求模式
                                          //将参数字符串转换成字节数组。
                byte[] postData = System.Text.Encoding.UTF8.GetBytes(param);

                request.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";  //设置ContntType ,这句很重要,否则无法传递参数

                request.ContentLength = postData.Length; //设置请求内容大小,当然就设置成我们的参数字节数据大小。

                Stream requestStream = request.GetRequestStream(); //获取请求流

                requestStream.Write(postData, 0, postData.Length); //将参数字节数组写入到请求流里

                requestStream.Close(); //关闭请求流

                HttpWebResponse response = (HttpWebResponse)request.GetResponse(); //执行请求,获取响应对象

                Stream stream = response.GetResponseStream(); //获取响应流

                StreamReader sr = new StreamReader(stream, Encoding.UTF8); //创建流读取对象

                string responseHTML = sr.ReadToEnd(); //读取响应流

                response.Close();  //关闭响应流

                return responseHTML;
            }
            catch(Exception ex)
            {
                return string.Empty;
            }
        }
URL : http://210.22.91.77:29015/v1/token/access?appId=f0ec96ad2c3848b5b810e7aadf369e2f×tamp=20170829162021&nonce=1234
xdashewan 2017-08-30
  • 打赏
  • 举报
回复 1
可能性有不少,直接搜索http 400对错误进行排除

111,112

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧