指定urlのリクエスト内容の取得

3079 ワード

分からないところがあればQQ群をプラスすることを歓迎します
14670545検討
///
        ///     url     
        ///
        ///
        ///
        ///
        public static string GetRemoteHtmlCodeByEncoding(string Url, string encode)
        {
            HttpWebResponse Result = null;

            try
            {
                HttpWebRequest Req = (HttpWebRequest) System.Net.WebRequest.Create(Url);
                Req.Method = "get";
                Req.ContentType = "application/x-www-form-urlencoded";
                Req.Credentials = CredentialCache.DefaultCredentials;
                Result = (HttpWebResponse) Req.GetResponse();

                StreamReader ReceiveStream = new StreamReader(Result.GetResponseStream(), Encoding.GetEncoding(encode));
                string OutPutString;
                try
                {
                    OutPutString = ReceiveStream.ReadToEnd();
                }
                catch
                {
                    OutPutString = string.Empty;
                }
                finally
                {
                    ReceiveStream.Close();
                }

                return OutPutString;
            }
            catch
            {
                return string.Empty;
            }
            finally
            {
                if (Result != null)
                {
                    Result.Close();
                }
            }
        }
/// 
        ///  POST          
        /// 
        ///    url
        ///     
        ///     
        /// 
        public static string Post_Http(string url, string postData, string encodeType)
        {
            HttpWebResponse myResponse = null;
            string strResult = null;
            try
            {
                Encoding encoding = Encoding.GetEncoding(encodeType);
                byte[] POST = encoding.GetBytes(postData);
                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
                myRequest.Method = "POST";
                myRequest.ContentType = "application/x-www-form-urlencoded";
                myRequest.ContentLength = POST.Length;
                Stream newStream = myRequest.GetRequestStream();
                newStream.Write(POST, 0, POST.Length); //  POST
                newStream.Close();
                //       
                myResponse = (HttpWebResponse)myRequest.GetResponse();
                StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.Default);
                strResult = reader.ReadToEnd();
            }
            catch (Exception ex)
            {
                strResult = ex.Message;
            }
            finally
            {
                if (myResponse != null)
                    myResponse.Close();
            }
            return strResult;
        }