[英]Getting error “ The remote server returned an error: (400) Bad Request.” on line WebResponse response = request.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.