HttpWebRequest的唯一而小提琴手正在运行的工作原理 [英] HttpWebRequest only works while fiddler is running

查看:200
本文介绍了HttpWebRequest的唯一而小提琴手正在运行的工作原理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在用下面的代码的一些问题。它会运行正常时是小提琴手,但它超时时提琴手没有运行。

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();



希望你们有一个答案。 (我读了SO另一个线程,但我没有答案的工作对我来说)

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

谢谢!

推荐答案

尝试用这个来读取响应流:

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;
    }


我刚才看到你ultimatley想要一个字符串从响应,所以用这个字节数组转换为字符串:

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);
}

这篇关于HttpWebRequest的唯一而小提琴手正在运行的工作原理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆