1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

C# Proxy connection closed unexpectedly

Discussion in 'C, C++, C#' started by Monrox, Oct 2, 2010.

  1. Monrox

    Monrox Power Member

    Joined:
    Apr 9, 2010
    Messages:
    615
    Likes Received:
    579
    I am getting a lot of webexceptions (The underlying connections was closed: The connection was closed unexpectedly) when using a httpwebrequest to connect through proxies... and am out of ideas.

    Anyne knows how to fix this? It's driving me crazy!

    Thanks
     
  2. daminkz

    daminkz Newbie

    Joined:
    Dec 22, 2009
    Messages:
    5
    Likes Received:
    0
    Show me the code and the errors.
     
  3. smack

    smack Junior Member

    Joined:
    Feb 1, 2010
    Messages:
    182
    Likes Received:
    78
    Occupation:
    Software Engineer/Evil Genius
    Location:
    inside .NET
    usually when you see connection errors like this related to proxy servers it's due to the proxies timing out or otherwise dropping connection for an unspecified reason.

    what kind of frequency are we talking here? and are you trying to connect over straight http protocol or SSL?

    -edit-

    could just be some dodgy, error prone proxies.
     
  4. Monrox

    Monrox Power Member

    Joined:
    Apr 9, 2010
    Messages:
    615
    Likes Received:
    579
    Well it's a simple httpwebrequest. When I try to chain proxies, the rate of this kind of exception is even 100%, even the working ones 'stop' working. This means something is wrong on my side or at least I think so.

    Anyway, I decided to try my luck with sockets instead we'll see how that goes.

    Hey smack, how would you join the buffers of Socket.Receive to get one big byte array? I am making a function to retrieve any resource and a string won't do without unneeded reconversion back to byte [].

    Generics.AddRange is adding empty bytes if the buffer is not full.
     
  5. daminkz

    daminkz Newbie

    Joined:
    Dec 22, 2009
    Messages:
    5
    Likes Received:
    0
    It's all speculation until you post some code and error messages.
     
  6. smack

    smack Junior Member

    Joined:
    Feb 1, 2010
    Messages:
    182
    Likes Received:
    78
    Occupation:
    Software Engineer/Evil Genius
    Location:
    inside .NET

    ah ok i've never worked with daisy chaining proxies in the framework.

    as far as the byte[] use an iterator loop (foreach)

    i think this should do what you're asking. i haven't used this for sockets, it's original purpose was for reconstructing an encryption key stored in two places. (i know it's in vb, but same concept)

    Code:
    Dim tempkey() As Byte = {1, 2, 4,...}
    Dim MyKey(31) As Byte
    Dim i As Integer
      For Each Bitt As Byte In tempkey
        MyKey.SetValue(Bitt, i)
        i += 1
       Next
    
      For Each Bitt As Byte In KeyBytes
        MyKey.SetValue(Bitt, i)
        i += 1
      Next
    
    since i knew the total length of the end byte[] prior to putting them together i was able to dimension the final array with an explicit value, you will probably have to resize it based on the size of the two arrays you're putting together.

    now for the response of the request method in the sockets namespace it's actually getting setup with the return with the inData from the SocketState.

    i know i'm probably rambling a bit at this point, it's quite early and i haven't had my requisite gallon of coffee yet, but hopefully this answers your questions.
     
    • Thanks Thanks x 1
  7. MakeLoot

    MakeLoot Registered Member

    Joined:
    Dec 14, 2010
    Messages:
    52
    Likes Received:
    2


    Here is code I've written to append byte arrays...



    public static byte[] byteAppend(byte[] destination, byte[] src)
    {
    if (destination == null) destination = new byte[0];

    byte[] newbytes = new byte[destination.Length + src.Length];
    destination.CopyTo(newbytes, 0);

    src.CopyTo(newbytes, destination.Length);

    return newbytes;
    }



    sample usage :

    byte[] a;
    byte[] b = new byte[100];
    byte[] c = new byte[200];
    byte[] d = new byte[300];

    a=(a,b);
    a=(a,c);
    a=(a,d);

    or
    a = (b,c);
    a = (a, d);

    .....

    hope that doesn't confuse :)