Authentication failed because the remote party has closed the transport stream. C# HTTP Post Request

0

i am trying a simple REST request to get the response. the code is failing at webRequest.GetRequestStream() with following error

System.Net.WebException HResult=0x80131509 Message=The underlying connection was closed: An unexpected error occurred on a send. Source=System StackTrace: at System.Net.HttpWebRequest.GetRequestStream(TransportContext& context) at System.Net.HttpWebRequest.GetRequestStream() at Com.VMware.Api.ApiClient.HttpPost(Uri uri, Dictionary`2 headers, String body) in E:\MyWork\CSPClientProject\Com.VMware\Com.VMware\ApiClient.cs:line 48 at Com.VMware.CSP.CSPAPIService.GetAccessTokenByAPIRefreshToken(String refreshToken) in E:\MyWork\CSPClientProject\Com.VMware\Com.VMware\CSP\CSPAPIService.cs:line 39 at RefreshAndAccessTokenSample.TestRefreshAndAccessTokenSample.Main(String[] args) in E:\MyWork\CSPClientProject\Com.VMware\RefreshAndAccessTokenSample\TestRefreshAndAccessTokenSample.cs:line 20

Inner Exception 1: IOException: Authentication failed because the remote party has closed the transport stream.

this is the full code

public Dictionary<int, string> HttpPost(Uri uri, Dictionary<String, String> headers, String body)
    {
        Dictionary<int, string> responseMap = new Dictionary<int, string>();
        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uri);
        webRequest.Method = "POST";
        foreach (KeyValuePair<string, string> kvp in headers)
        {
            if (kvp.Key.Equals("Content-Type"))
                webRequest.ContentType = kvp.Value;
            else if (kvp.Key.Equals("Accept"))
                webRequest.Accept = kvp.Value;
        }
        byte[] bytes = Encoding.UTF8.GetBytes(body);
        int contentLength = bytes.Length;
        webRequest.ContentLength = contentLength;
        Stream outputStream = webRequest.GetRequestStream();
        outputStream.Write(bytes, 0, bytes.Length);
        outputStream.Close();
        WebResponse response = (HttpWebResponse)webRequest.GetResponse();
        //create responseMap based upon response
        response.Close();
        return responseMap;
    }
c#
rest
api
asked on Stack Overflow Oct 24, 2018 by Saurabh Jha

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0