Note:
The same code when I am execution on dotnetfiddle.net where framework version is v4.7. It is working perfectly. While the code I have written is compatible with v3.5
I am consuming an API into .NET 3.5 using Visual Studio 2008 using WebRequest. Following are the details of API:
Method:POST
ConetntType:application/x-www-form-urlencoded
With Three Parameters.
Key Value
ApiKey string
ApiOwner string
RequestBody json string
I am using the following code pattern.
- WebRequest request = WebRequest.Create("https://hostname/RestWS/api/v1/order/orderPull");
- request.Method = "POST";
- string postData = "ApiOwner=sa&ApiKey=d564078......9530dec&RequestBody=";
- postData = postData + "{ \"orderNo\":\"o81075084\",\n \"statuses\":[],\n \"fromDate\":\"\",\n \"toDate\":\"\",\n \"pageNumber\":\"\",\n \"order_Location\":\"\",\n \"IsReplacementOrder\":\"\",\n \"orderSource\":\"\",\n \"paymentType\":[\"Prepaid\",\"COD\"]\n }";
- byte[] byteArray = Encoding.UTF8.GetBytes(postData);
- request.ContentType = "application/x-www-form-urlencoded";
- request.ContentLength = byteArray.Length;
- Stream dataStream = request.GetRequestStream(); //Error at this line
- dataStream.Write(byteArray, 0, byteArray.Length);
- dataStream.Close();
- WebResponse response = request.GetResponse();
- Console.WriteLine(((HttpWebResponse)response).StatusDescription);
- dataStream = response.GetResponseStream();
- StreamReader reader = new StreamReader(dataStream);
- string responseFromServer = reader.ReadToEnd();
- Console.WriteLine(responseFromServer);
- reader.Close();
- dataStream.Close();
- response.Close();
Exception
Inner Exception:
Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.