一般的な方法.NET HTTPリクエストGET /ポスト
パブリック クラスHttpHelper { /// <要約> /// 发起のHttp请求 /// </要約> /// <PARAM NAME = "requestDto"> 请求实体</ PARAM> /// <戻る> </戻ります> パブリック 静的 HttpReturnDto < オブジェクト > DoHttp(HttpRequestDto requestDto) { VARの MSG = 新しい HttpReturnDto < オブジェクト > (); もし(requestDto == nullの || 文字列 .IsNullOrEmpty(requestDto.Url)|| 文字列.IsNullOrEmpty(requestDto.Method)) { msg.IsSuccess = 偽。 msg.Message = " 请求参数没有全部提供" 。 リターンMSG。 } のHttpWebRequestのHttpRequest = GetHttpRequest(requestDto)。 してみてください { 場合(!HttpRequestを= ヌル) { VARの応答= (HttpWebResponseの)httpRequest.GetResponse(); VAR streamIn =response.GetResponseStream(); もし(streamIn =!ヌル) { VARのリーダー= 新しいStreamReaderを(streamIn)。 msg.Data = reader.ReadToEnd()。 reader.Close(); streamIn.Close(); response.Close(); msg.IsSuccessは = 真; msg.Message = " 执行成功" 。 } } } キャッチ(例外例) { msg.IsSuccess = 偽。 msg.Message = ex.Message; リターンMSG。 } 戻りMSG。 } /// <要約> /// 初始化HTTP请求 /// </要約> /// <PARAM NAME = "requestDto"> </ PARAM> /// <戻る> </戻り> プライベート 静的HttpWebRequestのGetHttpRequest( HttpRequestDto requestDto) { VAR設定= requestDto.HttpConfigDto?新しいHttpConfig(); HttpWebRequestののHttpRequest = (HttpWebRequestの)WebRequest.Create(requestDto.Url)。 httpRequest.Method = requestDto.Method。 httpRequest.Referer = config.Referer。 // 有些页面不设置用户代理信息则会抓取不到内容 httpRequest.UserAgent = config.UserAgent。 httpRequest.Timeout = config.Timeout。 httpRequest.Accept = config.Accept。 httpRequest.Headers.Set(" エンコード受け入れる" 、config.AcceptEncoding)。 httpRequest.ContentType =config.ContentType; httpRequest.KeepAlive = config.KeepAlive。 スイッチ(requestDto.Method.ToUpper()) { 場合 、 " POST " : requestDto.Data = requestDto.Data?"" ; VAR BDATA = Encoding.UTF8.GetBytes(requestDto.Data)。 httpRequest.ContentType = " アプリケーション/ xmlの;のcharset = UTF-8 " 。 httpRequest.ContentLength = bData.Length。 VAR streamOut =httpRequest.GetRequestStream(); streamOut.Write(BDATA、0 、bData.Length)。 streamOut.Close(); 休憩; } 戻りのHttpRequestを、 } } パブリック クラスHttpConfig { パブリック 文字列リファラー{ GET。セット; } /// <要約> /// 默认(テキスト/ HTML) /// </要約> パブリック 列のContentType { 得ます。セット; } パブリック 文字列{受け入れGETを、SET ;} パブリック 文字列 AcceptEncoding { GET、SET ;} /// <まとめ> /// タイムアウト(MS)100000デフォルト /// </要約> 公共 INTタイムアウト{ GET、SET ;} パブリック 文字列 {ユーザーエージェントGET、SET ;} /// <まとめ> /// POSTリクエスト、データかどうかのgzip圧縮 /// </要約> 公共 BOOL GZipCompress { GET、SET; } パブリック ブールキープアライブ{ 得ます。セット; } パブリック 文字列するCharacterSet { 得ます。セット; } パブリックHttpConfig() { この .Timeout = 21億。 この .ContentType = " text / htmlの;のcharset = " + Encoding.UTF8.WebName。 この .UserAgent = " Mozillaの/ 5.0(Windows NTの6.1; WOW64)のAppleWebKit / 537.36(ヤモリのようにKHTML、)クローム/ 33.0.1750.117サファリ/ 537.36 " 。 この .Accept =" text / htmlの、アプリケーション/ XHTML + xmlの、アプリケーション/ XML、Q = 0.9、画像/ WEBP、* / *; Q = 0.8 " 。 この .AcceptEncoding = " GZIP、デフレート" 。 この .GZipCompressは= 偽; この .KeepAliveは= 真; この .CharacterSet = " UTF-8 " ; } } /// <要約> /// 返回信息实体 /// </要約> パブリック クラスHttpRequestDto { /// <要約> ///要求アドレス /// </要約> 公共の 文字列のURL { GET、SET ;} // 要求データ パブリック 文字列データ{ GET、SET ;} /// <まとめ> /// 要求メソッドPOST / GET /// </まとめ> パブリック 文字列方法、{ GET ; SET ;} /// <要約> /// 要求メソッドPOST / GET /// </要約> 公共 HttpConfig HttpConfigDto { GET、SET;} } /// <まとめ> /// 戻り情報エンティティ /// </要約> /// <typeParam名= "T"> </ typeParam> パブリック クラス HttpReturnDto <T> { パブリックHttpReturnDto() { isSuccess = falseに、 メッセージ = " 操作が失敗" ; } // 成功したかどうかを パブリック BOOL isSuccess { GET、SET ;} // コード パブリック 文字コードを{ GET; SET ;} // 情報 パブリック 文字列メッセージ{ GET、SET ;} // 戻りデータ 公共 T {データGET、SETを;} }