http Web请求500服务器错误?

时间:2015-04-17 21:10:50

标签: 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");
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的代码给出了同样的错误。

然后我尝试了Rest Easy插件并将数据发布到api并且工作正常并且没有给出任何错误并创建了帐户。

这可能有什么问题?我做错了什么或丢失了什么?第一个代码也可以正常使用Method PATCH

0 个答案:

没有答案