所以我只是想创建一个使用WebClient
一个基本的堆栈溢出的客户。当我按原样运行程序时,即使我睡觉并等待,我也会得到一个空字符串结果。然而,当我打开Fiddler2程序工作...我所要做的就是打开Fiddler ...这里是相关的代码。C#Web客户端<开放式和封闭式>(单独的结果)
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; }
}
另外值得注意的是菲德勒导致 当我在浏览器提琴手加载http://api.stackoverflow.com/1.1/questions显示
GET http://api.stackoverflow.com/1.1/questions 200 OK(应用/ JSON)
和
GET http://api.stackoverflow.com/favicon.ico 503服务不可用(text/html的)
当我加载它在我的节目虽然只有这表明
GET http://api.stackoverflow.com/1.1/questions 200 OK(应用/ JSON)
对于浏览器与应用的东西,浏览器总是要求的favicon.ico,使他们可以把自己的图像浏览器选项卡上 - 这是正常的,而这里的错误是没有什么大不了的。 – 2013-03-08 19:55:32
好吧,至少我们可以统治那个诡计了。 – 2013-03-08 19:56:22
您是否尝试过同步运行而不是异步运行以查看会发生什么?当你这样做时结果是否完整?你所描述的听起来像是即使整个事件正在发射,结果也不是真的回来 - 但这并没有什么意义。另外,也许尝试在该事件处理程序中抛出一个'Thread.Sleep(2000)'或其他东西,以防万一这可能是问题 - 如果它适用于“睡眠”,但不是没有它,那么真正质朴的东西是继续。 – 2013-03-08 19:57:52