一般的な方法.NET HTTPリクエストGET /ポスト

 

一般的な方法.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 { GETSET ;} 

        ///  <まとめ> 
        /// タイムアウト(MS)100000デフォルト
         ///  </要約> 
        公共 INTタイムアウト{ GETSET ;} 

        パブリック 文字列 {ユーザーエージェントGETSET ;} 

        ///  <まとめ> 
        /// POSTリクエスト、データかどうかのgzip圧縮
         ///  </要約> 
        公共 BOOL GZipCompress { GETSET; } 

        パブリック ブールキープアライブ{ 得ますセット; } 

        パブリック 文字列する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 { GETSET ;}
         // 要求データ
        パブリック 文字列データ{ GETSET ;}
         ///  <まとめ> 
        /// 要求メソッドPOST / GET
         ///  </まとめ> 
        パブリック 文字列方法、{ GET ; SET ;}
         ///  <要約> 
        /// 要求メソッドPOST / GET
         ///  </要約> 
        公共 HttpConfig HttpConfigDto { GETSET;} 
    } 

    ///  <まとめ> 
    /// 戻り情報エンティティ
     ///  </要約> 
    ///  <typeParam名= "T"> </ typeParam> 
    パブリック クラス HttpReturnDto <T> 
    { 
        パブリックHttpReturnDto()
        { 
            isSuccess = falseに
            メッセージ = " 操作が失敗" ; 
        } 
        // 成功したかどうかを
        パブリック BOOL isSuccess { GETSET ;}
         // コード
        パブリック 文字コードを{ GET; SET ;}
         // 情報
        パブリック 文字列メッセージ{ GETSET ;}
         // 戻りデータ
        公共 T {データGETSETを;} 
    }

 

おすすめ

転載: www.cnblogs.com/jiangqw/p/12121230.html