コード例 #1
0
        /// <summary>
        /// Post And Get
        /// </summary>
        /// <param name="targetUrl">URL</param>
        /// <param name="method">Method</param>
        /// <param name="accept">Accept</param>
        /// <param name="contentType">Content Type</param>
        /// <param name="userAgent">User Agent</param>
        /// <param name="encoding">Encoding</param>
        /// <param name="cookies">Cookies</param>
        /// <param name="strPostdata">Post Data</param>
        public string GetRequest(string targetUrl, string method, string accept, string contentType, string userAgent, Encoding encoding, CookieContainer cookies, string strPostdata)
        {
            this.SetRequest(targetUrl, method, accept, contentType, userAgent, encoding, cookies);
            if (null != strPostdata && methodType.POST == this.Method)
            {
                byte[] buffer = this.Encoding.GetBytes(strPostdata);
                this.Request.ContentLength = buffer.Length;
                this.Request.GetRequestStream().Write(buffer, 0, buffer.Length);
                this.Request.GetRequestStream().Close();
            }
            #region 得到请求的response
            string resultData = null;
            try
            {
                using (this.Response = (HttpWebResponse)this.Request.GetResponse())
                {
                    Stream ResposeStream = this.Response.GetResponseStream();
                    if (Encoding.Default == this.Encoding && this.AutoGetEncoding)
                    {
                        MemoryStream _stream     = HelperBase.CopyStream(ResposeStream);
                        byte[]       RawResponse = _stream.ToArray();
                        string       temp        = Encoding.Default.GetString(RawResponse, 0, RawResponse.Length);

                        string charset = HelperBase.GetString(temp, "<meta([^<]*)\\bcharset\\b\\s*=\\s*([^<';\"]*)[\"']", 2);
                        if (!string.IsNullOrEmpty(charset))
                        {
                            this.Encoding = Encoding.GetEncoding(charset);
                            resultData    = this.Encoding.GetString(RawResponse);
                        }
                        else
                        {
                            if (this.Response.CharacterSet.ToLower().Trim() == "iso-8859-1")
                            {
                                encoding = Encoding.Default;
                            }
                            else
                            {
                                if (string.IsNullOrEmpty(this.Response.CharacterSet.Trim()))
                                {
                                    encoding = Encoding.UTF8;
                                }
                                else
                                {
                                    encoding = Encoding.GetEncoding(this.Response.CharacterSet);
                                }
                            }
                        }
                    }
                    else
                    {
                        string ContentEncoding = this.Response.ContentEncoding;
                        if (null != ContentEncoding && ContentEncoding.Equals("gzip", StringComparison.InvariantCultureIgnoreCase))
                        {
                            using (this.Reader = new StreamReader(new GZipStream(ResposeStream, CompressionMode.Decompress), this.Encoding))
                            {
                                resultData = this.Reader.ReadToEnd();
                            }
                        }
                        else
                        {
                            using (this.Reader = new StreamReader(ResposeStream, this.Encoding))
                            {
                                resultData = this.Reader.ReadToEnd();
                            }
                        }
                    }
                }
            }
            catch (WebException ex)
            {
                if (ex.Status == WebExceptionStatus.ProtocolError &&
                    ex.Response != null)
                {
                    var resp = (HttpWebResponse)ex.Response;
                    if (resp.StatusCode == HttpStatusCode.NotFound)
                    {
                        resultData = "Error:Not found";
                    }
                    else
                    {
                        resultData = "Error:Forbidden";
                    }
                }
                else
                {
                    resultData = "Error:Unknow";
                }
            }
            return(resultData);

            #endregion
        }