繁体   English   中英

WebResponse response = response .GetResponse()在发布数据和获取响应时给出错误请求错误

[英]WebResponse response =response .GetResponse() giving Bad Request error when POSTing data and GETing response

我想检索其GUID与数据库匹配的用户(类)的详细信息。 我在ASP.NET MVC项目中使用WCF REST ful Web服务。

但是,当发布GUID并获取user数据作为响应时, WebResponse response =response .GetResponse()给出错误请求错误400。

这是Interface

        [OperationContract]
        [WebInvoke( Method="POST",
                    RequestFormat = WebMessageFormat.Json,
                    ResponseFormat = WebMessageFormat.Json,
                    BodyStyle = WebMessageBodyStyle.WrappedRequest,
                    UriTemplate = "Edit/{guid}"
                   )]
        User Edit(string guid);

这是Controller Action

public ActionResult Edit(string guid)
        {
            if (guid != "0")
            {

                WebRequest Request = WebRequest.Create("http://localhost:65000/Service1.svc/Edit/guid");
                // Set the Method property of the request to POST.
                Request.Method = "POST";
                // Create POST data and convert it to a byte array.
                string postData = guid;
                byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(postData);
                // Set the ContentType property of the WebRequest.
                Request.ContentType = "application/x-www-form-urlencoded";
                // Set the ContentLength property of the WebRequest.
                Request.ContentLength = byteArray.Length;
                // Get the request stream.
                Stream dataStream = Request.GetRequestStream();
                // Write the data to the request stream.
                dataStream.Write(byteArray, 0, byteArray.Length);
                // Close the Stream object.
                dataStream.Close();
                // Get the response.
                WebResponse response = Request.GetResponse(); //THIS LINE GIVING BAD REQUEST ERROR 400

                // Get the stream containing content returned by the server.
                dataStream = response.GetResponseStream();
                // Open the stream using a StreamReader for easy access.
                StreamReader reader = new StreamReader(dataStream);
                // Read the content.
                string responseFromServer = reader.ReadToEnd();

                User UserData = JsonConvert.DeserializeObject<User>(responseFromServer);
                ViewData["UserData"] = UserData;

                //  Clean up the streams.
                reader.Close();
                dataStream.Close();
                response.Close();

                return View(UserData);
            }

            return View(new User());
        }

PS:下面的代码工作正常,可以使用WebGet检索所有用户。

我认为这个问题是只有当我想POST像GUID和读取一些数据response

 public ActionResult Index()
        {
        WebRequest Request = WebRequest.Create("http://localhost:65000/Service1.svc/RetrieveAll");
        // Get the response.
        WebResponse Response = Request.GetResponse();
        // Get the stream containing content returned by the server.
        Stream DataStream = Response.GetResponseStream();
        // Open the stream using a StreamReader for easy access.
        StreamReader Reader = new StreamReader(DataStream);
        // Read the content.
        string ResponseFromServer = Reader.ReadToEnd();
        // Display the content.
        List<User> UserData = JsonConvert.DeserializeObject<List<User>>(ResponseFromServer);
        ViewData["UserData"] = UserData;
        // Clean up the streams.
        Reader.Close();
        DataStream.Close();
        Response.Close();
        return View();
    }

好像网址不正确

WebRequest Request = WebRequest.Create(“ http:// localhost:65000 / Service1.svc / Edit / guid ”);

应该是(URL中带有guid)

WebRequest Request = WebRequest.Create(“ http:// localhost:65000 / Service1.svc / Edit / 6F9619FF-8B86-D011-B42D-00CF4FC964FF ”);

我认为问题是您没有将字符串guid用作变量,而是将其用作文本,我认为正确的网址是:

WebRequest request = WebRequest.Create("http://localhost:65000/Service1.svc/Edit/" + guid);

如果在“”中使用单词“ guid”,它将按原样使用一个单词,而不是用作变量guid内的值。 希望它有所帮助;)

作者:

我发现问题在于dB连通性,同时检索用户的数据,问题中提到的代码是正确的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM