2

I'm having some issues with the following code. It'll run fine when Fiddler is on, but it times out when Fiddler isn't running.

IWebProxy proxy = websiterequester.Proxy;
websiterequester = (HttpWebRequest)WebRequest.Create("http://website.com/");
websiterequester.CookieContainer = cookieJar;
websiterequester.Method = "GET";
websiterequester.Referer = "http://website.com/";
if (websiterequester.Proxy != null)
{
  websiterequester.Proxy = null;
}

try
{
  objStream1 = websiterequester.GetResponse().GetResponseStream();
}
catch (WebException ex)
{
  return "oops";
}

objReader1 = new StreamReader(objStream1);
string thiscamebacks = objReader1.ReadToEnd();

Hope you guys have an answer. (I read another thread on SO, but I none of the answers worked for me)

Thanks!

Artemix
  • 2,113
  • 2
  • 23
  • 34
  • might not be related, but dispose `objStream1` after using it. I've seen similar issues due missing dispose. – Claudio Redi Sep 18 '12 at 21:31
  • I tried that, but that didn't fix it. – Stefan Dorresteijn Sep 18 '12 at 21:32
  • Is it possible that you've configured Fiddler as a system-wide proxy (I think this is the default), and when you close it the settings aren't getting removed cleanly from Internet Options? Therefore it's trying to contact the Fiddler proxy (on 8888) but it no longer exists? Check Internet Options in Control Panel. – Sir Crispalot Sep 18 '12 at 22:39
  • Unfortunately not, we've got customers using it and it's not working for them either. – Stefan Dorresteijn Sep 18 '12 at 23:19

1 Answers1

0

Try using this to read the response stream:

    private byte[] ReadWebResponse(WebResponse response)
    {
        byte[] bytes = null;
        if(response == null) return null;

        using(Stream responseStream = response.GetResponseStream())
        {
            using(BinaryReader readStream = new BinaryReader(responseStream))
            {
                using(MemoryStream memoryStream = new MemoryStream())
                {
                    byte[] buffer = new byte[256];
                    int count;
                    int totalBytes = 0;
                    while((count = readStream.Read(buffer, 0, 256)) > 0)
                    {
                        memoryStream.Write(buffer, 0, count);
                        totalBytes += count;
                    }
                    memoryStream.Position = 0;
                    bytes = new byte[totalBytes];
                    memoryStream.Read(bytes, 0, totalBytes);
                }
            }
        }
        return bytes;
    }

[Edit] I just saw that you ultimatley wanted a string from the response, so use this to convert the byte array to a string:

/// <summary>
/// Returns the byte array as a string, or null
/// </summary>
public static string GetByteString(byte[] b)
{
    if (b == null) return null;
    return Encoding.UTF8.GetString(b);
}
Matt Klein
  • 7,856
  • 6
  • 45
  • 46
  • Can you break the `objStream1 = websiterequester.GetResponse().GetResponseStream();` into two parts, then find out which method it's hanging on? Or, I guess, more generally, can you tell us which method is hanging? – Matt Klein Sep 18 '12 at 21:56
  • It hangs on the GetResponse() method – Stefan Dorresteijn Sep 18 '12 at 21:58
  • Here are a few more things to try that I found others suggesting and I have implemented in my working solution. 1) I see that you are using websiterequester before the .Create() command. Make sure you only use one request per .Create(). 2) Try setting the ContentLength to 0 for GET calls. – Matt Klein Sep 18 '12 at 22:41
  • [ran out of space] 3) Try changing security (just for testing) by calling the static methods `System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;` and `System.Net.ServicePointManager.ServerCertificateValidationCallback = MyValidationCallback; private bool MyValidationCallback(object sender, System.Security.Cryptography.X509Certificates.X509Certificate cert, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors err) { return true; }` – Matt Klein Sep 18 '12 at 22:45
  • It's not #1, it's not #2, it's not #3. – Stefan Dorresteijn Sep 18 '12 at 23:24