だから私はを使用して基本的なスタックオーバーフロークライアントを作成しようとしています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; }
}
また、ブラウザにhttp://api.stackoverflow.com/1.1/questionsをロードすると、フィドラーの結果が表示されます。
GET http://api.stackoverflow.com/1.1/questions 200 OK(application / json)
と
GET http://api.stackoverflow.com/favicon.ico 503 Service Unavailable(text / html)
これだけが表示されますが、プログラムにロードすると
GET http://api.stackoverflow.com/1.1/questions 200 OK(application / json)