weixin_44606182
weixin_44606182
采纳率100%
2019-06-27 17:49

.net HttpWebRequest/ajax() 访问 java Post接口 对方接口一直提示参数不全

已采纳

Httpwebreques请求代码 :

        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://www.baoyanghui.com/Car_2_0/api/open/order/submit");
            request.Method = "POST";
            request.Timeout = 8000;
            request.KeepAlive = false;
            request.Host = "www.baoyanghui.com";
            request.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
            request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36";
            //byte[] payload = Encoding.UTF8.GetBytes(tparams);
            //request.ContentLength = payload.Length;
            using (Stream stream = request.GetRequestStream())
            {

                StreamWriter myStreamWriter = new StreamWriter(stream, Encoding.GetEncoding("UTF-8"));
                myStreamWriter.Write(tparams);
                //stream.Write(payload, 0, payload.Length);
            }

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            if (response.StatusCode == HttpStatusCode.OK)
            {
                Stream stream = response.GetResponseStream();
                System.IO.StreamReader streamReader = new StreamReader(stream, Encoding.GetEncoding("UTF-8"));
                StringBuilder content = new StringBuilder(streamReader.ReadToEnd());// 读取流字符串内容
                var res = JsonConvert.DeserializeObject<BranchList>(content.ToString());
                if (res.code != "0")
                {
                    throw new Exception("订单:" + orderId + "。推送失败:" + res.message);
                }
            }
            else
            {
                throw new Exception("订单:" + orderId + "。推送失败:网络错误");
            }

ajax请求代码 :

        $.ajax({
            type: "post",
            async: false,
            data: models,
            url: "http://www.baoyanghui.com/Car_2_0/api/open/order/submit",
            dataType: "json",
            contentType: "application/x-www-form-urlencoded; charset=UTF-8",
            success: function (json) {
                console.log("success:");
                console.log(json);
                /*
                $.post(location.href, { ids: orderIds.join(','), companyNames: companyNames.join(','), shipOrderNumbers: shipOrderNumbers.join(','), needShip: needShip }, function (result) {
                    loading.close();
                    if (result.success) {
                        $.dialog.succeedTips("操作成功!", function () { window.location.href = "./Management" });
                    }
                    else
                        $.dialog.errorTips("操作失败," + result.msg);
                });*/
            },
            error: function (e) {
                console.log(e);
            }
        });

两次都请求成功了,就是参数一直提示不全,但我确定参数是都有传递的,包括必须的和可选的,都传了。

求问!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • caozhy 回答这么多问题就耍赖把我的积分一笔勾销了 2年前

    fiddler抓包对比你的程序和浏览器两者提交的参数区别

    点赞 评论 复制链接分享