代码之家  ›  专栏  ›  技术社区  ›  DotNetRussell

带有fidler2<打开和关闭>的C#WebClient(单独的结果)

  •  2
  • DotNetRussell  · 技术社区  · 13 年前

    所以我只是想用 WebClient 。当我按原样运行程序时,即使我在睡觉和等待,我也会得到一个空字符串结果。然而,当我打开Fiddler2时,程序会工作。。。我所要做的就是打开Fiddler。。。这是相关代码。

    public partial class MainWindow : Window
    {
        public  ObservableCollection<question>  questions { get; set; }
    
        public MainWindow() 
        {
            questions = new ObservableCollection<question>();
            this.DataContext = this;
    
            InitializeComponent();
        }
    
        void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            MessageBox.Show(e.Result); //Right here is the difference. When 
    
            <BREAK POINT HERE OR IT BREAKS>
    
            string data = data = e.Result.Substring(e.Result.IndexOf("class=\"question-summary narrow\"") + 31);
            string content = data.Substring(0, data.IndexOf("class=\"question-summary narrow\""));
            string v, a, t, b, tgs, link;
            questions.Add(new question
                {
                    //votes = v,
                    //answers = a,
                    //title = t.ToUpper(),
                    //body = b,
                    ////tags = tgs
                    //href = link
                 });
        }
    
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            WebClient wc = new WebClient();
            wc.DownloadStringAsync(new Uri(@"http://api.stackoverflow.com/1.1/questions"));
            wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wc_DownloadStringCompleted);
        }
    }
    public class question
    {
        public string votes { get; set; }
        public string answers { get; set; }
        public string title { get; set; }
        public string body { get; set; }
        public string tags { get; set; }
        public string href { get; set; }
    }
    

    同样值得注意的是fidler的结果 当我加载时 http://api.stackoverflow.com/1.1/questions 在浏览器中,fiddler显示

    得到 http://api.stackoverflow.com/1.1/questions 200 OK(应用程序/json)

    得到 http://api.stackoverflow.com/favicon.ico 503服务不可用(text/html)

    当我把它加载到我的程序中时,尽管只有这个显示

    得到 http://api.stackoverflow.com/1.1/questions 200 OK(应用程序/json)

    1 回复  |  直到 13 年前
        1
  •  4
  •   Joe Enos    13 年前

    看起来问题出在API本身。尽管你没有告诉它你接受GZipped的内容,但它无论如何都是GZipped,显然Fiddler会处理这个问题并为你解压。在你的应用程序中,你必须通过解压缩内容来处理这个问题。以下是如何做到这一点的一个简单示例:

    var wc = new WebClient();
    var bytes = wc.DownloadData(new Uri(@"http://api.stackoverflow.com/1.1/questions"));
    string responseText;
    using (var outputStream = new MemoryStream())
    {
        using (var memoryStream = new MemoryStream(bytes))
        {
            using (var gzip = new GZipStream(memoryStream, CompressionMode.Decompress))
            {
                byte[] buffer = new byte[1024];
                int numBytes;
                while ((numBytes = gzip.Read(buffer, 0, buffer.Length)) > 0)
                {
                    outputStream.Write(buffer, 0, numBytes);
                }
            }
            responseText = Encoding.UTF8.GetString(outputStream.ToArray());
        }
    }
    
    Console.WriteLine(responseText);
    

    不管它是否总是GZipped,谁知道呢?你可以检查Content Encoding HTTP头,看看它是否 gzip ,如果是,则运行此代码;如果不是,则可以将字节直接转换为文本。