Praveen Kumar

Praveen Kumar

  • 259
  • 6.6k
  • 2.3m

Error while consuming API into .NET 3.5 with VS2008 into Console App

Dec 9 2020 11:34 PM
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.
  1. WebRequest request = WebRequest.Create("https://hostname/RestWS/api/v1/order/orderPull");  
  2.                 request.Method = "POST";  
  3.                 string postData = "ApiOwner=sa&ApiKey=d564078......9530dec&RequestBody=";  
  4.                 postData = postData + "{ \"orderNo\":\"o81075084\",\n \"statuses\":[],\n \"fromDate\":\"\",\n \"toDate\":\"\",\n \"pageNumber\":\"\",\n \"order_Location\":\"\",\n \"IsReplacementOrder\":\"\",\n \"orderSource\":\"\",\n  \"paymentType\":[\"Prepaid\",\"COD\"]\n }";  
  5.                 byte[] byteArray = Encoding.UTF8.GetBytes(postData);  
  6.                 request.ContentType = "application/x-www-form-urlencoded";  
  7.                 request.ContentLength = byteArray.Length;  
  8.                 Stream dataStream = request.GetRequestStream();  //Error at this line
  9.                 dataStream.Write(byteArray, 0, byteArray.Length);  
  10.                 dataStream.Close();  
  11.                 WebResponse response = request.GetResponse();  
  12.                 Console.WriteLine(((HttpWebResponse)response).StatusDescription);  
  13.                 dataStream = response.GetResponseStream();  
  14.                 StreamReader reader = new StreamReader(dataStream);  
  15.                 string responseFromServer = reader.ReadToEnd();  
  16.                 Console.WriteLine(responseFromServer);  
  17.                 reader.Close();  
  18.                 dataStream.Close();  
  19.                 response.Close();  
Exception
 
Inner Exception:
Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.
 

Answers (5)