Esempio n. 1
0
        //public object SendRequest(string requestContent)
        //{
        //    string retXml = GetTogether.Utility.RequestHelper.GetRequest(this.Url, requestContent, "POST", "application/x-www-form-urlencoded", this.Timeout);
        //    return GetTogether.Utility.Xml.XmlHelper.FormatXml(retXml);
        //}
        public string SendRequest(string requestContent, RequestProperty properties)
        {
            if (properties.Accept != null) properties.Accept = properties.Accept.Trim();
            if (properties.Address != null) properties.Address = properties.Address.Trim();
            if (properties.BasicAuthPassword != null) properties.BasicAuthPassword = properties.BasicAuthPassword.Trim();
            if (properties.BasicAuthUserName != null) properties.BasicAuthUserName = properties.BasicAuthUserName.Trim();
            if (properties.ContentType != null) properties.ContentType = properties.ContentType.Trim();
            if (properties.Expect != null) properties.Expect = properties.Expect.Trim();
            if (properties.MediaType != null) properties.MediaType = properties.MediaType.Trim();
            if (properties.Method != null) properties.Method = properties.Method.Trim();
            if (properties.Referer != null) properties.Referer = properties.Referer.Trim();
            if (properties.TransferEncoding != null) properties.TransferEncoding = properties.TransferEncoding.Trim();
            if (properties.UserAgent != null) properties.UserAgent = properties.UserAgent.Trim();

            if (string.IsNullOrEmpty(properties.Address)) properties.Address = this.Url;
            if (properties.Timeout == 0) properties.Timeout = this.Timeout;
            if (string.IsNullOrEmpty(properties.Method)) properties.Method = "post";
            if (string.IsNullOrEmpty(properties.ContentType)) properties.ContentType = "text/xml; charset=utf-8";//application/x-www-form-urlencoded

            return RequestHelper.SendRequest(requestContent, properties);
        }
Esempio n. 2
0
        public static string SendRequest(string requestContent, RequestProperty properties)
        {
            string responseXml = string.Empty;
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(properties.Address);
            if (properties.Timeout > 0) req.Timeout = properties.Timeout;
            byte[] request_bytes = System.Text.Encoding.UTF8.GetBytes(requestContent);
            req.Method = properties.Method;//"POST";
            req.ContentType = properties.ContentType;//"text/xml,application/x-www-form-urlencoded
            req.ContentLength = request_bytes.Length;
            if (properties.Headers != null && properties.Headers.Length > 0)
            {
                foreach (string s in properties.Headers)
                {
                    string[] headerInfo = s.Split('|');
                    if (headerInfo.Length == 2)
                        req.Headers[headerInfo[0]] = headerInfo[1];
                }
            }
            CredentialCache cache = new CredentialCache();
            bool flag = false;
            if (!string.IsNullOrEmpty(properties.BasicAuthUserName) && !string.IsNullOrEmpty(properties.BasicAuthPassword))
            {
                cache.Add(new Uri(properties.Address), "Basic", new NetworkCredential(properties.BasicAuthUserName, properties.BasicAuthPassword));
                flag = true;
            }
            if (properties.UseDefaultCredential)
            {
                cache.Add(new Uri(properties.Address), "NTLM", (NetworkCredential)CredentialCache.DefaultCredentials);
                flag = true;
            }
            if (flag)
            {
                req.Credentials = cache;
            }
            req.UserAgent = properties.UserAgent;
            req.AllowAutoRedirect = properties.AllowAutoRedirect;
            req.AllowWriteStreamBuffering = properties.AllowWriteStreamBuffering;
            req.KeepAlive = properties.KeepAlive;
            req.Pipelined = properties.Pipelined;
            req.PreAuthenticate = properties.PreAuthenticate;
            req.Referer = properties.Referer;
            req.SendChunked = properties.SendChunked;
            req.TransferEncoding = properties.TransferEncoding;
            req.Accept = properties.Accept;
            req.Expect = properties.Expect;
            req.MediaType = properties.MediaType;

            Stream request_stream = req.GetRequestStream();
            request_stream.Write(request_bytes, 0, request_bytes.Length);
            request_stream.Close();
            try
            {
                HttpWebResponse response = (HttpWebResponse)req.GetResponse();
                responseXml = RequestHelper.GetResponse(response);
                response.Close();
            }
            catch (WebException exception)
            {
                if (exception.Response != null)
                {
                    responseXml = RequestHelper.GetResponse((HttpWebResponse)exception.Response);
                }
                else
                {
                    responseXml = exception.ToString();
                }
            }
            catch (Exception exception2)
            {
                responseXml = exception2.ToString();
            }
            return responseXml;
        }