C# http Web请求提供500服务器错误?
所以我尝试使用这段代码将数据发送到API以创建一个新帐户,但它崩溃了,并给出了一个500服务器错误C# http Web请求提供500服务器错误?,c#,httpwebrequest,C#,Httpwebrequest,所以我尝试使用这段代码将数据发送到API以创建一个新帐户,但它崩溃了,并给出了一个500服务器错误 var Account= new Account(); Account.name = "Test Name"; //Serialize string json = JsonConvert.SerializeObject(Account); WebRequest request = WebRequest.Create("https://api.example.com/profiles/1");
var Account= new Account();
Account.name = "Test Name";
//Serialize
string json = JsonConvert.SerializeObject(Account);
WebRequest request = WebRequest.Create("https://api.example.com/profiles/1");
request.ContentType = "text/json";
request.Method = "POST";
using (streamWriter = new StreamWriter(httpRequest.GetRequestStream()))
{
streamWriter.Write(json);
}
using (httpWebResponse = httpRequest.GetResponse() as HttpWebResponse)
{
streamReader = new StreamReader(httpWebResponse.GetResponseStream());
var jsonResponse = streamReader.ReadToEnd();
}
然后我尝试了msdn网站的例子:
// Create a request using a URL that can receive a post.
WebRequest request = WebRequest.Create ("https://api.example.com/profiles/1");
// Set the Method property of the request to POST.
request.Method = "POST";
// Create POST data and convert it to a byte array.
byte[] byteArray = Encoding.UTF8.GetBytes (json);
// Set the ContentType property of the WebRequest.
request.ContentType = "application/json";
// 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 ();
// Display the status.
Console.WriteLine (((HttpWebResponse)response).StatusDescription);
// 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 ();
// Display the content.
Console.WriteLine (responseFromServer);
// Clean up the streams.
reader.Close ();
dataStream.Close ();
response.Close ();
来自msdn的代码给出了相同的错误
然后我尝试了RESTEasy插件并将数据发布到api中,该插件运行良好,没有给出任何错误,并创建了帐户
这有什么问题吗?我是做错了还是错过了什么?另外,第一个代码在使用方法补丁时运行良好首先,您的Url应该包含以下方案:http或其他任何内容……使用fiddler或wireshark跟踪请求并确保其匹配。