C# HttpWebRequest POST

Frank Schneider picture Frank Schneider · Oct 16, 2017 · Viewed 7.7k times · Source

I try to send Data to a REST Service by POST, but it get at access GetResponseStream() "Von der Übertragungsverbindung können keine Daten gelesen werden: Eine vorhandene Verbindung wurde vom Remotehost geschlossen."

I dont Know why, i look in the internet but i cannot solve.

    #region MakeRequestPost
    //=========================================================
    //
    //=========================================================
    public string MakeRequestPost()
    {
        string responseValue = string.Empty;
        string postData;
        HttpWebRequest request;

        endPoint = "https://gymapi.test.egym.de/egym-rest/gymapi/user/publish?gymId=2995&accesstoken=************************";

        postData = "{\"eMail\":\"[email protected]\",\"firstName\":\"Frank\",\"lastName\":\"Schneider\"}";
        byte[] byteArray = Encoding.UTF8.GetBytes(postData); //json

        request = (HttpWebRequest)WebRequest.Create(endPoint);
        request.Method = "POST";
        request.ContentType = "application/json; charset=UTF-8";
        request.Accept = "application/json";
        request.UserAgent = "Studio Manager";
        request.ContentLength = byteArray.Length;

        Stream dataStream = request.GetRequestStream();
        dataStream.Write(byteArray, 0, byteArray.Length);
        dataStream.Close();

        WebResponse response = request.GetResponse();

        dataStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(dataStream);
        string responseFromServer = reader.ReadToEnd();
        dataStream.Close();

        response.Close();

        return (responseFromServer);
    }
    #endregion

I hope some can help me.

I try with postman and it works 200 OK

thanks

Answer

Jiangping Lu picture Jiangping Lu · Jul 5, 2018

try to change the endPoint to this:

endPoint = "http://gymapi.test.egym.de/egym-rest/gymapi/user/publish?gymId=2995&accesstoken=************************";