濮阳杆衣贸易有限公司

主頁 > 知識庫 > HttpWebRequest和HttpWebResponse用法小結

HttpWebRequest和HttpWebResponse用法小結

熱門標簽:凱立德地鐵站地圖標注 上海400客服電話怎么申請 手機外呼系統(tǒng)什么原理 銀行信貸電話機器人 天津電銷外呼系統(tǒng)違法嗎 合肥ai電銷機器人費用 滄州電銷外呼系統(tǒng)價格 400電話個人能不能辦理 溫州外呼系統(tǒng)招商
最近公司拓展市場異常迅猛,數(shù)周之類開出去幾十套系統(tǒng),雖然系統(tǒng)名字不一樣,但各個內容相似。由于時間緊迫,很多開出去的系統(tǒng)
出現(xiàn)各種神奇的錯誤,當初雖然有記錄錯誤日志,然而很多客戶使用的是自己的服務器和數(shù)據庫,出了問題我們并不能立即掌握信息,
因此決定做一個捕獲所有系統(tǒng)的異常并保存到自家數(shù)據庫中。
實現(xiàn)思路
在每個系統(tǒng)出寫入報告錯誤代碼(找個合理的理由,比如系統(tǒng)免費升級) -> 自家服務器接收并處理錯誤報告 -> 反饋用戶(解決掉BUG就行,不要太聲揚)
基礎回顧
---參考msdn
1.HttpWebRequest類:提供WebRequest類的Http特定的實現(xiàn)。
HttpWebRequest 類對 WebRequest 中定義的屬性和方法提供支持,也對使用戶能夠直接與使用 HTTP 的服務器交互的附加屬性和方法提供支持。
不要使用構造函數(shù)創(chuàng)建HttpWebRequest實例,請使用System.Net.WebRequest.Create(URI uriString)來創(chuàng)建實例,如果URI是Http://或Https://,
返回的是HttpWebRequest對象。(建立請求特定URI的對象)
當向資源發(fā)送數(shù)據時,GetRequestStream方法返回用于發(fā)送數(shù)據的Stream對象。(獲取請求數(shù)據的流對象)
GetResponse方法向RequestUri屬性指定的資源發(fā)出同步請求并返回包含該響應的HttpWebResponse。(獲取來自internet的響應)
實例講解
1.遠程請求并返回響應
復制代碼 代碼如下:

/// summary>
/// 報告系統(tǒng)錯誤
/// /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();
//要提交的字符串數(shù)據
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 "連接錯誤";
}
finally
{
requestWriter.Close();
requestWriter = null;
}
responseData = WebResponseGet(webRequest);
webRequest = null;
return responseData;
}
catch
{
return "未知錯誤";
}
}

復制代碼 代碼如下:

/// 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 "連接錯誤";
}
finally
{
webRequest.GetResponse().GetResponseStream().Close();
responseReader.Close();
responseReader = null;
}
return responseData;
}

2.遠程服務器讀取流
復制代碼 代碼如下:

_context = HttpContext.Current;
Stream stream = _context.Request.InputStream; //獲取當前傳入Http輸入流
long length = stream.Length;
byte[] data = _context.Request.BinaryRead((int)length);//對當前輸入流進行指定字節(jié)數(shù)的二進制讀取
string strContent = Encoding.UTF8.GetString(data);//解碼為UTF8編碼形式的字符串

代碼講解到此結束,一些相關補充:
1.HttpWebRequest對象有一些相關設置屬性,如Method(發(fā)送方式),TimeOut(請求超時時間),ContentType(Http標頭的值)等等。
2.若遠程接收頁面出錯,該如何調試,很簡單,只需寫入下面的代碼:
復制代碼 代碼如下:

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);
}

當獲取服務器響應出錯時,捕捉錯誤,最終打印出錯誤即可。
您可能感興趣的文章:
  • C#中HttpWebRequest的用法詳解
  • C#采用HttpWebRequest實現(xiàn)保持會話上傳文件到HTTP的方法
  • HttpWebRequest的常見錯誤使用TcpClient可避免
  • HttpWebRequest出錯.Section=ResponseHeader Detail=CR
  • .net core并發(fā)請求發(fā)送HttpWebRequest的坑解決

標簽:洛陽 酒泉 金華 怒江 赤峰 白城 七臺河 溫州

巨人網絡通訊聲明:本文標題《HttpWebRequest和HttpWebResponse用法小結》,本文關鍵詞  HttpWebRequest,和,HttpWebResponse,;如發(fā)現(xiàn)本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統(tǒng)采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《HttpWebRequest和HttpWebResponse用法小結》相關的同類信息!
  • 本頁收集關于HttpWebRequest和HttpWebResponse用法小結的相關信息資訊供網民參考!
  • 推薦文章
    富源县| 稷山县| 仪征市| 若羌县| 鸡泽县| 老河口市| 柞水县| 克什克腾旗| 夏河县| 彭水| 桃园县| 上林县| 井冈山市| 云和县| 安康市| 正阳县| 阿瓦提县| 丰都县| 泗洪县| 安多县| 台中县| 涟水县| 射阳县| 鄂尔多斯市| 青州市| 独山县| 福海县| 汉沽区| 岳阳市| 普安县| 会宁县| 泽州县| 清苑县| 宜州市| 金溪县| 景德镇市| 雅江县| 东明县| 安康市| 罗定市| 武定县|