Exemplo n.º 1
0
        /// <summary>
        /// 发送到指定的URL的请求
        /// </summary>
        /// <param name="url">请求的URL</param>
        /// <param name="postData">要发送的数据</param>
        /// <param name="requestType">请求类型 POST GET</param>
        /// <param name="contentType">请求的内容类型。默认值是'application/x-www-form-urlencoded'.</param>
        /// <param name="userAgent">请求 UserAgent</param>
        /// <param name="keepAlive">KeepAlive true/false</param>
        /// <param name="credentials">使用的凭据</param>
        /// <param name="proxy">使用代理</param>
        /// <param name="timeout">超时</param>
        /// <param name="encoding">设置响应编码</param>
        /// <param name="postDataEncoding">设置POST数据编码</param>
        /// <returns>返回请求的数据</returns>
        public static string SendRequest(string url, string postData, string requestType, string contentType, string userAgent, bool? keepAlive, ICredentials credentials, IWebProxy proxy, int? timeout, Encoding encoding, Encoding postDataEncoding, WebHeaderCollection webHeaderCollection = null) {
            if (!String.IsNullOrEmpty(url)) {
                HttpWebRequest webReq;
                HttpWebResponse webResp = null;
                if (postDataEncoding == null) postDataEncoding = Encoding.UTF8;

                try {
                    webReq = (HttpWebRequest)WebRequest.Create(url);
                    webReq.Method = requestType;

                    if (webHeaderCollection.IsNotNull()) webReq.Headers = webHeaderCollection;
                    if (credentials.IsNotNull()) webReq.Credentials = credentials;
                    if (proxy.IsNotNull()) webReq.Proxy = proxy;
                    if (timeout.IsNotNull()) webReq.Timeout = (int)timeout;
                    webReq.ServicePoint.Expect100Continue = false;
                    //webReq.PreAuthenticate = true;
                    //webReq.AllowWriteStreamBuffering = true;
                    webReq.ContentType = !String.IsNullOrEmpty(contentType) ? contentType : "text/xml"; // "application/x-www-form-urlencoded";
                    if (!String.IsNullOrEmpty(userAgent)) webReq.UserAgent = userAgent;
                    if (keepAlive.IsNotNull()) webReq.KeepAlive = (bool)keepAlive;

                    byte[] data = !String.IsNullOrEmpty(postData) ? postDataEncoding.GetBytes(postData) : postDataEncoding.GetBytes(String.Empty);
                    webReq.ContentLength = data.Length;
                    if (requestType.ToUpper() != "GET") {
                        Stream writer = webReq.GetRequestStream();
                        writer.Write(data, 0, data.Length);
                        writer.Close();
                    }

                    webResp = (HttpWebResponse)webReq.GetResponse();

                    string response;
                    using (var receiveStream = webResp.GetResponseStream()) {
                        using (var readStream = new StreamReader(receiveStream, encoding))
                            response = readStream.ReadToEnd();
                    }

                    return response;
                } finally {
                    if (webResp.IsNotNull()) webResp.Close();
                }
            }

            throw new Exception("必须指定一个有效的URL。");
        }