HttpWebRequest外部服务

public static string CallService(string strXml)

        {

            Uri uri = new Uri(Settings.SettingWebServiceUrl);

            string postData = strXml;

            string result = null;

            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);

 

            request.KeepAlive = true;

            request.Timeout = 60000;

            request.ContentType = "text/xml";

            request.Accept =

                "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";

            request.Method = "POST";

            byte[] data = Encoding.UTF8.GetBytes(postData);

            request.ContentLength = data.Length;

            try

            {

                request.ClientCertificates.Add(

                    new X509Certificate2(

                        Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Settings.CertificateFileName),

                        Settings.CertificatePwd));

                System.Net.ServicePointManager.CertificatePolicy = new MyPolicy();

 

                if (Settings.IfQC != "true")

                {

                    WebProxy proxy = new WebProxy();

                    proxy.Address = new Uri(Settings.ProxyUrl);

 

                    string username = Settings.ProxyUserID;

                    string password = Settings.ProxyPassword;

 

                    proxy.Credentials = new System.Net.NetworkCredential(username, password);

                    request.Proxy = proxy;

                }

                using (Stream writer = request.GetRequestStream())

                {

                    writer.Write(data, 0, data.Length);

                }

 

                WebResponse response = request.GetResponse();

                using (Stream responseStream = response.GetResponseStream())

                {

                    using (StreamReader reader = new StreamReader(responseStream, Encoding.UTF8))

                    {

                        result = reader.ReadToEnd();

                    }

                }

            }

            catch (Exception ex)

            {

                throw ex;

            }

 

            return result;

        }

posted @ 2016-03-04 10:37  anranstl  阅读(144)  评论(0编辑  收藏  举报