HttpWebRequest和HttpWebResponse用法小結(jié)
出現(xiàn)各種神奇的錯(cuò)誤,當(dāng)初雖然有記錄錯(cuò)誤日志,然而很多客戶使用的是自己的服務(wù)器和數(shù)據(jù)庫(kù),出了問(wèn)題我們并不能立即掌握信息,
因此決定做一個(gè)捕獲所有系統(tǒng)的異常并保存到自家數(shù)據(jù)庫(kù)中。
實(shí)現(xiàn)思路
在每個(gè)系統(tǒng)出寫入報(bào)告錯(cuò)誤代碼(找個(gè)合理的理由,比如系統(tǒng)免費(fèi)升級(jí)) -> 自家服務(wù)器接收并處理錯(cuò)誤報(bào)告 -> 反饋用戶(解決掉BUG就行,不要太聲揚(yáng))
基礎(chǔ)回顧
---參考msdn
1.HttpWebRequest類:提供WebRequest類的Http特定的實(shí)現(xiàn)。
HttpWebRequest 類對(duì) WebRequest 中定義的屬性和方法提供支持,也對(duì)使用戶能夠直接與使用 HTTP 的服務(wù)器交互的附加屬性和方法提供支持。
不要使用構(gòu)造函數(shù)創(chuàng)建HttpWebRequest實(shí)例,請(qǐng)使用System.Net.WebRequest.Create(URI uriString)來(lái)創(chuàng)建實(shí)例,如果URI是Http://或Https://,
返回的是HttpWebRequest對(duì)象。(建立請(qǐng)求特定URI的對(duì)象)
當(dāng)向資源發(fā)送數(shù)據(jù)時(shí),GetRequestStream方法返回用于發(fā)送數(shù)據(jù)的Stream對(duì)象。(獲取請(qǐng)求數(shù)據(jù)的流對(duì)象)
GetResponse方法向RequestUri屬性指定的資源發(fā)出同步請(qǐng)求并返回包含該響應(yīng)的HttpWebResponse。(獲取來(lái)自internet的響應(yīng))
實(shí)例講解
1.遠(yuǎn)程請(qǐng)求并返回響應(yīng)
/// <summary>
/// 報(bào)告系統(tǒng)錯(cuò)誤
/// </summary>
/// <param name="ex"></param>
/// <returns></returns>
public static string Sys_ReportError(Exception ex)
{
try
{
//要提交表單的URI字符串
string uriString = "http://localhost/Sys_ReportError.aspx";
HttpContext context = HttpContext.Current;
if (context == null) return string.Empty;
string targetSite = ex.TargetSite.ToString();
string stackTrace = ex.StackTrace;
string friendlyMsg = ex.Message;
string errorPage = context == null || context.Request == null ? "" : context.Request.Url.ToString();
string projectName = Config.Sys_Title();
//要提交的字符串?dāng)?shù)據(jù)
string postString = "targetSite=" + HttpUtility.UrlEncode(targetSite);
postString += "&stackTrace=" + HttpUtility.UrlEncode(stackTrace);
postString += "&friendlyMsg=" + HttpUtility.UrlEncode(friendlyMsg);
postString += "&errorPage=" + HttpUtility.UrlEncode(errorPage);
postString += "&projectName=" + HttpUtility.UrlEncode(projectName);
postString += "&key=" + "";
HttpWebRequest webRequest = null;
StreamWriter requestWriter = null;
string responseData = "";
webRequest = System.Net.WebRequest.Create(uriString) as HttpWebRequest;
webRequest.Method = "POST";
webRequest.ServicePoint.Expect100Continue = false;
webRequest.Timeout = 1000 * 60;
webRequest.ContentType = "application/x-www-form-urlencoded";
//POST the data.
requestWriter = new StreamWriter(webRequest.GetRequestStream());
try
{
requestWriter.Write(postString);
}
catch (Exception ex2)
{
return "連接錯(cuò)誤";
}
finally
{
requestWriter.Close();
requestWriter = null;
}
responseData = WebResponseGet(webRequest);
webRequest = null;
return responseData;
}
catch
{
return "未知錯(cuò)誤";
}
}
/// <summary>
/// Process the web response.
/// </summary>
/// <param name="webRequest">The request object.</param>
/// <returns>The response data.</returns>
public static string WebResponseGet(HttpWebRequest webRequest)
{
StreamReader responseReader = null;
string responseData = "";
try
{
responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream());
responseData = responseReader.ReadToEnd();
}
catch
{
return "連接錯(cuò)誤";
}
finally
{
webRequest.GetResponse().GetResponseStream().Close();
responseReader.Close();
responseReader = null;
}
return responseData;
}
2.遠(yuǎn)程服務(wù)器讀取流
_context = HttpContext.Current;
Stream stream = _context.Request.InputStream; //獲取當(dāng)前傳入Http輸入流
long length = stream.Length;
byte[] data = _context.Request.BinaryRead((int)length);//對(duì)當(dāng)前輸入流進(jìn)行指定字節(jié)數(shù)的二進(jìn)制讀取
string strContent = Encoding.UTF8.GetString(data);//解碼為UTF8編碼形式的字符串
代碼講解到此結(jié)束,一些相關(guān)補(bǔ)充:
1.HttpWebRequest對(duì)象有一些相關(guān)設(shè)置屬性,如Method(發(fā)送方式),TimeOut(請(qǐng)求超時(shí)時(shí)間),ContentType(Http標(biāo)頭的值)等等。
2.若遠(yuǎn)程接收頁(yè)面出錯(cuò),該如何調(diào)試,很簡(jiǎn)單,只需寫入下面的代碼:
HttpWebResponse res = null;
WebResponse response = null;
try
{
WebResponse response = webRequest.GetResponse();
}
catch (WebException ex1)
{
res = (HttpWebResponse)ex1.Response;
}
finally
{
StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
string strhtml = sr.ReadToEnd();
HttpContext.Current.Response.Write(strhtml);
}
當(dāng)獲取服務(wù)器響應(yīng)出錯(cuò)時(shí),捕捉錯(cuò)誤,最終打印出錯(cuò)誤即可。
相關(guān)文章
asp.net實(shí)現(xiàn)的MD5加密和DES加解密算法類完整示例
這篇文章主要介紹了asp.net實(shí)現(xiàn)的MD5加密和DES加解密算法類,結(jié)合完整實(shí)例形式分析了asp.net實(shí)現(xiàn)MD5加密算法及DES加密和解密的相關(guān)技巧,需要的朋友可以參考下2016-07-07.NET中的狀態(tài)機(jī)庫(kù)Stateless的操作流程
本文給大家介紹了.NET中的狀態(tài)機(jī)庫(kù)Stateless, 使用它我們可以很容易的定義出自己業(yè)務(wù)需要的狀態(tài)機(jī),或者基于狀態(tài)機(jī)的工作流,本文大部分的內(nèi)容都來(lái)自官方Github,有興趣的同學(xué)可以深入研究一下2021-12-12asp.net 使用js分頁(yè)實(shí)現(xiàn)異步加載數(shù)據(jù)
這篇文章主要介紹了asp.net使用js分頁(yè)實(shí)現(xiàn)異步加載數(shù)據(jù),需要的朋友可以參考下2014-04-04asp.net core3.1cookie和jwt混合認(rèn)證授權(quán)實(shí)現(xiàn)多種身份驗(yàn)證方案
身份驗(yàn)證是確定用戶身份的過(guò)程。 授權(quán)是確定用戶是否有權(quán)訪問(wèn)資源的過(guò)程。本文主要介紹了asp.net core3.1cookie和jwt混合認(rèn)證授權(quán)實(shí)現(xiàn)多種身份驗(yàn)證方案,感興趣的可以了解一下2021-09-09ASP .NET Core API發(fā)布與部署以及遇到的坑和解決方法
這篇文章主要介紹了ASP .NET Core API發(fā)布與部署以及遇到的坑和解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08asp.net 從客戶端中檢測(cè)到有潛在危險(xiǎn)的 Request.Form 值錯(cuò)誤解
asp.net程序運(yùn)行時(shí)出現(xiàn)以下錯(cuò)誤: “/news”應(yīng)用程序中的服務(wù)器錯(cuò)誤。2009-05-05ASP.Net 請(qǐng)求響應(yīng)流程簡(jiǎn)述
ASP.Net 請(qǐng)求響應(yīng)流程簡(jiǎn)述,需要的朋友可以參考下。2012-01-01ASP.NET Core中快速構(gòu)建PDF文檔的步驟分享
這篇文章主要給大家介紹了關(guān)于ASP.NET Core中快速構(gòu)建PDF文檔的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用ASP.NET Core具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12asp.net 刪除,更新數(shù)據(jù)庫(kù)方法
asp.net 刪除,更新數(shù)據(jù)庫(kù)方法2009-07-07