HTTPWebResponse Error: Unable to read data from the transport connection

I have a console app that is making a web request to my localhost and I keep getting this error:

"System.IO.IOException: Unable to read data from the transport connection. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host\r\n at System.Net.Sockets.Socket.BeginReceive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags, AsyncCallback callback, Object state)\r\n at System.Net.Sockets.NetworkStream.BeginRead(Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state)\r\n --- End of inner exception stack trace ---\r\n at System.Net.Sockets.NetworkStream.BeginRead(Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state)\r\n at System.Net.Connection.BeginRead(Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state)\r\n at System.Net.ConnectStream.InternalBeginRead(Int32 bytesToRead, NestedSingleAsyncResult castedAsyncResult, Boolean fromCallback)\r\n at System.Net.ConnectStream.BeginReadWithoutValidation(Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state)\r\n at System.Net.ConnectStream.BeginRead(Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state)\r\n at System.Net.ConnectStream.Read(Byte[] buffer, Int32 offset, Int32 size)\r\n at System.IO.StreamReader.ReadBuffer(Char[] userBuffer, Int32 userOffset, Int32 desiredChars, Boolean& readToUserBuffer)\r\n at System.IO.StreamReader.Read(Char[] buffer, Int32 index, Int32 count)\r\n at System.IO.StreamReader.ReadToEnd()\r\n at Run.Class1.Main(String[] args) in c:\\documents and settings\\####\\my documents\\c#\\class1.cs:line 41"

The web app is in Asp.Net 2.0 and is a file system web application (not sure if this has anything to do with it).

Here is the code in the console app:

byte[] buffer;
buffer = Encoding.UTF8.GetBytes("KEY=Test");
HttpWebRequest wReq = (HttpWebRequest)WebRequest.Create("http://localhost:1991/TestApp/Test.aspx");
wReq.Method = "POST";
wReq.ContentType = "text/html";
wReq.ContentLength = buffer.Length;
using (Stream reqst = wReq.GetRequestStream()) // add form data to request stream
{
reqst.Write(buffer, 0, buffer.Length);
}
using (HttpWebResponse wRes = (HttpWebResponse)wReq.GetResponse()) // send request, get response
{
string result;
using (Stream resst = wRes.GetResponseStream()) // get HTTP response
{
StreamReader sr = new StreamReader(resst);
result = sr.ReadToEnd();
}
Console.WriteLine(result);
}

In my webpage, i have no controls, session is disabled.

The following is the code in my webpage:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="SignIn" EnableSessionState="False" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

<title>Test</title>

</head>

<body>

<form id="form1" runat="server">

</form>

</body>

</html>

Here is the Code Behind page

protected void Page_Load(object sender, EventArgs e)

{

Response.Cache.SetCacheability(HttpCacheability.NoCache);

Response.Cache.SetAllowResponseInBrowserHistory(false);

Response.ContentType = "text/html";

Response.Clear();

Response.BufferOutput = true;

Response.Write("KEY=DATA");

Response.Flush();

}



Answer this question

HTTPWebResponse Error: Unable to read data from the transport connection

  • CSH

    Your console.app code is correct. As for your web page - I wasn't able to make your example work; one thing I noticed you are calling your file test.aspx.cs (CodeFile="Test.aspx.cs"). I created another aspx page using this code and it worked with the code for your console app.

    <Script language="C#" runat=server>
    void Page_Load(object sender, EventArgs e)
    {
    Response.Write("KEY=DATA");
    }
    </Script>

    For asp related questions you can post in this forum
    http://forums.microsoft.com/MSDN/ShowForum.aspx ForumID=46&SiteID=1

    Let me know if this helped
    Mariya


  • Ravs Kaur _ MS

    hi,

    i see you write in both sides in your client

    reqst.Write(buffer, 0, buffer.Length);

    and in your page

    Response.Write("KEY=DATA");

    and also you send data before your response so your connection wasn't open yet

    using (Stream reqst = wReq.GetRequestStream()) // add form data to request stream
    {
    reqst.Write(buffer, 0, buffer.Length);
    }
    using (HttpWebResponse wRes = (HttpWebResponse)wReq.GetResponse()) // send request, get response

     

    hope this helps

     

     



  • wessamzeidan

    Seems like it is the Cassini file system web server. I put the web application under IIS on my localhost and the console app now works. Not sure why this don't work on the file system web server. Thanks for the help though.
  • Muzzzy

    I'm only writing reqst.Write(buffer, 0, buffer.Length); in the client app, the Response.Write("KEY=DATA"); is written in the webpage and is what should be returned in the ResponseStream.

    Basically I'm posting a webrequest in the client with the data "Key=Test" and then in the response from the webpage I should get back "KEY=DATA". Could you help me with this solution with a code example


  • HTTPWebResponse Error: Unable to read data from the transport connection