locked
The underlying connection was closed: The connection was closed unexpectedly. RRS feed

  • Question

  • I get this exception: The underlying connection was closed: The connection was closed unexpectedly.

    making a simple httpwebrequest.

    This is the c# code:

     

    using System;

    using System.Net;

    namespace testrequest {

    class Program {

    static void Main(string[] args) {

    Uri u = new Uri("http://www.careers.icaew.com/school-students-leavers/Entry-routes/University-and-higher-education/Undergraduate-Partnership-Programme");

    HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(u);

    HttpWebResponse Response = (HttpWebResponse)Request.GetResponse();

    }

    }

    }

     

    I have no problems requesting the url in IE9, Chrome, Firefox or Perl.

    I have no influence on the server setup.

    Can anyone help me solve this problem?

     

    Thanks,

    Jesper

    Tuesday, October 4, 2011 7:47 AM

All replies

  • Jesper

    What version of .net Framework you are using ?

    Try below two line of snipet

    Request.KeepAlive = false;
    Request.ProtocolVersion = HttpVersion.Version10;
    HttpWebResponse Response = (HttpWebResponse)Request.GetResponse();

     Below link may help you.

    http://support.microsoft.com/kb/915599


    Lingaraj Mishra
    Tuesday, October 4, 2011 12:34 PM
  • I changed the program to:

     

    Uri u = new Uri("http://www.careers.icaew.com/school-students-leavers/Entry-routes/University-and-higher-education/Undergraduate-Partnership-Programme");

    HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(u);

    Request.KeepAlive = false;

    Request.ProtocolVersion = HttpVersion.Version10;

    HttpWebResponse Response = (HttpWebResponse)Request.GetResponse();

     

    But still get the same error.

     

    .net framework is 4.

     

    Thanks,

    Jesper

    Wednesday, October 5, 2011 8:23 AM
  • Jesper,

    Are u seeing any message in event log.Can you post some detailed innerexception you are geting.Not sure is it bcz of security or default proxy setting.

     


    Lingaraj Mishra
    Wednesday, October 5, 2011 12:06 PM
  • Hi Lingaraj,

    would you mind trying the mentioned code to see if you can request the page from your "environment"?

     

    No inner exception.

     

    detailed network log:

     

    System.Net Verbose: 0 : [2776] WebRequest::Create(http://www.careers.icaew.com/school-students-leavers/Entry-routes/University-and-higher-education/Undergraduate-Partnership-Programme)

    System.Net Verbose: 0 : [2776] HttpWebRequest#49582139::HttpWebRequest(http://www.careers.icaew.com/school-students-leavers/Entry-routes/University-and-higher-education/Undergraduate-Partnership-Programme#1967817789)

    System.Net Information: 0 : [2776] Current OS installation type is 'Client'.

    System.Net Information: 0 : [2776] RAS supported: True

    System.Net Verbose: 0 : [2776] Exiting HttpWebRequest#49582139::HttpWebRequest() 

    System.Net Verbose: 0 : [2776] Exiting WebRequest::Create() -> HttpWebRequest#49582139

    System.Net Verbose: 0 : [2776] HttpWebRequest#49582139::GetResponse()

    System.Net Error: 0 : [2776] Can't retrieve proxy settings for Uri 'http://www.careers.icaew.com/school-students-leavers/Entry-routes/University-and-higher-education/Undergraduate-Partnership-Programme'. Error code: 12180.

    System.Net Information: 0 : [2776] Associating HttpWebRequest#49582139 with ServicePoint#30887001

    System.Net Information: 0 : [2776] Associating Connection#1968367 with HttpWebRequest#49582139

    System.Net.Sockets Verbose: 0 : [2776] Socket#5720734::Socket(InterNetwork#2)

    System.Net.Sockets Verbose: 0 : [2776] Exiting Socket#5720734::Socket() 

    System.Net.Sockets Verbose: 0 : [2776] Socket#47056750::Socket(InterNetworkV6#23)

    System.Net.Sockets Verbose: 0 : [2776] Exiting Socket#47056750::Socket() 

    System.Net.Sockets Verbose: 0 : [2776] Socket#5720734::Connect(145:80#-1848495740)

    System.Net.Sockets Information: 0 : [2776] Socket#5720734 - Created connection from 192.168.20.190:54326 to 212.49.210.145:80.

    System.Net.Sockets Verbose: 0 : [2776] Exiting Socket#5720734::Connect() 

    System.Net.Sockets Verbose: 0 : [2776] Socket#47056750::Close()

    System.Net.Sockets Verbose: 0 : [2776] Socket#47056750::Dispose()

    System.Net.Sockets Verbose: 0 : [2776] Exiting Socket#47056750::Close() 

    System.Net Information: 0 : [2776] Connection#1968367 - Created connection from 192.168.20.190:54326 to 212.49.210.145:80.

    System.Net Information: 0 : [2776] Associating HttpWebRequest#49582139 with ConnectStream#63378184

    System.Net Information: 0 : [2776] HttpWebRequest#49582139 - Request: GET /school-students-leavers/Entry-routes/University-and-higher-education/Undergraduate-Partnership-Programme HTTP/1.1

     

    System.Net.Sockets Verbose: 0 : [2776] Socket#5720734::Send()

    System.Net.Sockets Verbose: 0 : [2776] Data from Socket#5720734::Send

    System.Net.Sockets Verbose: 0 : [2776] 00000000 : 47 45 54 20 2F 73 63 68-6F 6F 6C 2D 73 74 75 64 : GET /school-stud

    System.Net.Sockets Verbose: 0 : [2776] 00000010 : 65 6E 74 73 2D 6C 65 61-76 65 72 73 2F 45 6E 74 : ents-leavers/Ent

    System.Net.Sockets Verbose: 0 : [2776] 00000020 : 72 79 2D 72 6F 75 74 65-73 2F 55 6E 69 76 65 72 : ry-routes/Univer

    System.Net.Sockets Verbose: 0 : [2776] 00000030 : 73 69 74 79 2D 61 6E 64-2D 68 69 67 68 65 72 2D : sity-and-higher-

    System.Net.Sockets Verbose: 0 : [2776] 00000040 : 65 64 75 63 61 74 69 6F-6E 2F 55 6E 64 65 72 67 : education/Underg

    System.Net.Sockets Verbose: 0 : [2776] 00000050 : 72 61 64 75 61 74 65 2D-50 61 72 74 6E 65 72 73 : raduate-Partners

    System.Net.Sockets Verbose: 0 : [2776] 00000060 : 68 69 70 2D 50 72 6F 67-72 61 6D 6D 65 20 48 54 : hip-Programme HT

    System.Net.Sockets Verbose: 0 : [2776] 00000070 : 54 50 2F 31 2E 31 0D 0A-48 6F 73 74 3A 20 77 77 : TP/1.1..Host: ww

    System.Net.Sockets Verbose: 0 : [2776] 00000080 : 77 2E 63 61 72 65 65 72-73 2E 69 63 61 65 77 2E : w.careers.icaew.

    System.Net.Sockets Verbose: 0 : [2776] 00000090 : 63 6F 6D 0D 0A 43 6F 6E-6E 65 63 74 69 6F 6E 3A : com..Connection:

    System.Net.Sockets Verbose: 0 : [2776] 000000A0 : 20 43 6C 6F 73 65 0D 0A-0D 0A                   :  Close....

    System.Net.Sockets Verbose: 0 : [2776] Exiting Socket#5720734::Send() -> 170#170

    System.Net Information: 0 : [2776] ConnectStream#63378184 - Sending headers

    {

    Host: www.careers.icaew.com

    Connection: Close

    }.

    System.Net.Sockets Verbose: 0 : [2776] Socket#5720734::Receive()

    System.Net.Sockets Verbose: 0 : [2776] Data from Socket#5720734::Receive

    System.Net.Sockets Verbose: 0 : [2776] 00000000 : 48 54 54 50 2F 31 2E 31-20 33 30 31 20 4D 6F 76 : HTTP/1.1 301 Mov

    System.Net.Sockets Verbose: 0 : [2776] 00000010 : 65 64 20 50 65 72 6D 61-6E 65 6E 74 6C 79 0D 0A : ed Permanently..

    System.Net.Sockets Verbose: 0 : [2776] 00000020 : 4C 6F 63 61 74 69 6F 6E-3A 20 68 74 74 70 3A 2F : Location: http:/

    System.Net.Sockets Verbose: 0 : [2776] 00000030 : 2F 63 61 72 65 65 72 73-2E 69 63 61 65 77 2E 63 : /careers.icaew.c

    System.Net.Sockets Verbose: 0 : [2776] 00000040 : 6F 6D 2F 73 63 68 6F 6F-6C 2D 73 74 75 64 65 6E : om/school-studen

    System.Net.Sockets Verbose: 0 : [2776] 00000050 : 74 73 2D 6C 65 61 76 65-72 73 2F 45 6E 74 72 79 : ts-leavers/Entry

    System.Net.Sockets Verbose: 0 : [2776] 00000060 : 2D 72 6F 75 74 65 73 2F-55 6E 69 76 65 72 73 69 : -routes/Universi

    System.Net.Sockets Verbose: 0 : [2776] 00000070 : 74 79 2D 61 6E 64 2D 68-69 67 68 65 72 2D 65 64 : ty-and-higher-ed

    System.Net.Sockets Verbose: 0 : [2776] 00000080 : 75 63 61 74 69 6F 6E 2F-55 6E 64 65 72 67 72 61 : ucation/Undergra

    System.Net.Sockets Verbose: 0 : [2776] 00000090 : 64 75 61 74 65 2D 50 61-72 74 6E 65 72 73 68 69 : duate-Partnershi

    System.Net.Sockets Verbose: 0 : [2776] 000000A0 : 70 2D 50 72 6F 67 72 61-6D 6D 65 0D 0A          : p-Programme..

    System.Net.Sockets Verbose: 0 : [2776] Exiting Socket#5720734::Receive() -> 173#173

    System.Net Information: 0 : [2776] Connection#1968367 - Received status line: Version=1.1, StatusCode=301, StatusDescription=Moved Permanently.

    System.Net.Sockets Verbose: 0 : [2776] Socket#5720734::Receive()

    System.Net.Sockets Verbose: 0 : [2776] Data from Socket#5720734::Receive

    System.Net.Sockets Verbose: 0 : [2776] 0000008D :                                                 : 

    System.Net.Sockets Verbose: 0 : [2776] Exiting Socket#5720734::Receive() -> 0#0

    System.Net.Sockets Verbose: 0 : [2776] Socket#5720734::Dispose()

    System.Net Error: 0 : [2776] Exception in the HttpWebRequest#49582139:: - The underlying connection was closed: The connection was closed unexpectedly.

     

    Regards,

    Jesper

    • Edited by j.j.persson Wednesday, October 5, 2011 5:59 PM
    Wednesday, October 5, 2011 5:58 PM
  • This is a 301 response, the url you have is temporality forwarded to the new location, change your code to use the new address indicated in the response.

    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    Thursday, October 6, 2011 12:29 AM
  • I wish it was as simple as a 301 response.

    Problem is that an exception is thrown, so you never get the response object to see that a 301 is what the server responds.

    Setting the Request.AllowAutoRedirect to true/false makes no difference.

     

    Regards,

    Jesper


    • Edited by j.j.persson Thursday, October 6, 2011 5:09 AM
    Thursday, October 6, 2011 5:08 AM
  • Try run fildder2 and check the response, did the server cut the connection or your connection timeout is too small?  

    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    Thursday, October 6, 2011 5:33 AM
  • Hi

    I got it.

    Remove www from your url. I mean your code should look like

                Uri u = new Uri("http://careers.icaew.com/school-students-leavers/Entry-routes/University-and-higher-education/Undergraduate-Partnership-Programme");
                HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(u);
                HttpWebResponse Response = (HttpWebResponse)Request.GetResponse();
    

    Tested in my system .Works fine.

    Let us know your result.


    Lingaraj Mishra
    Thursday, October 6, 2011 5:40 AM
  • I actually knew that c# can request the url without the www., but that is not a solution :-(

    I need c# to automatically follow the redirect or at least give me the response telling me what url to "get next".

    The uri is just one of many links that I request and I cannot "change" the links on individual basis.


    Regards, Jesper
    Thursday, October 6, 2011 5:52 AM