本文整理汇总了C#中HttpClient.TryReadResponseException方法的典型用法代码示例。如果您正苦于以下问题:C# HttpClient.TryReadResponseException方法的具体用法?C# HttpClient.TryReadResponseException怎么用?C# HttpClient.TryReadResponseException使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HttpClient
的用法示例。
在下文中一共展示了HttpClient.TryReadResponseException方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: btnRun_Click
private async void btnRun_Click(object sender, EventArgs e)
{
if( btnRun.Enabled == false )
return;
btnRun.Enabled = false;
if( CheckWebsiteIsRunning("http://www.fish-ajax-cors.com/") == false // CrosClientWebSite1
|| CheckWebsiteIsRunning("http://www.fish-mvc-demo.com/") == false ) { // DemoWebSite1
btnRun.Enabled = true;
return;
}
_httpLog.Clear();
txtHttpLog.Text = string.Empty;
txtTestResult.Text = string.Empty;
StringBuilder sb = new StringBuilder();
int count = 0;
foreach( ListViewItem item in this.listView1.Items ) {
item.ImageIndex = 0;
}
foreach( ListViewItem item in this.listView1.Items ) {
if( item.Checked ) {
count++;
MethodInfo m = (MethodInfo)item.Tag;
item.ImageIndex = 1;
try {
object instance = Activator.CreateInstance(m.DeclaringType);
Task task = m.Invoke(instance, null) as Task;
if( task != null )
await task;
//Task.Run(() => m.Invoke(instance, null)).Wait();
item.ImageIndex = 2;
}
catch( Exception ex ) {
item.ImageIndex = 3;
string errorMessage = null;
WebException webException = ex as WebException;
if( webException != null ) {
HttpClient client = new HttpClient();
string html = client.TryReadResponseException(webException);
errorMessage = GetHtmlTitle(html);
}
sb.AppendLine("Method: " + m.DeclaringType.FullName + "." + m.Name)
.AppendLine("---------------------------------------------------")
.Append(errorMessage ?? ex.GetBaseException().Message)
.AppendLine("\r\n---------------------------------------------------\r\n\r\n");
}
}
}
btnRun.Enabled = true;
if( count == 0 ) {
txtTestResult.Text = "没有需要执行的测试用例。";
}
else {
if( sb.Length == 0 )
txtTestResult.Text = "全部测试用例已通过。";
else
txtTestResult.Text = sb.ToString();
}
txtHttpLog.Text = _httpLog.ToString();
_httpLog.Clear();
}