码迷,mamicode.com
首页 > Web开发 > 详细

HTTP POS

时间:2018-10-12 14:02:27      阅读:33      评论:0      收藏:0      [点我收藏+]

标签:ons   utf8   encoding   method   ext   lse   pos   finally   bre   

string result = ""; 
            HttpWebRequest request = null;
            HttpWebResponse response = null;
            Stream reqStream = null;
            StreamReader sr = null;
            try
            {  
                request = (HttpWebRequest)WebRequest.Create(url); 
                request.Method = "POST";
                request.Timeout = timeout * 1000;
                request.KeepAlive = false;  
                request.ContentType = "text/xml";
                byte[] data = System.Text.Encoding.UTF8.GetBytes(xml);
                request.ContentLength = data.Length;  
                reqStream = request.GetRequestStream();
                reqStream.Write(data, 0, data.Length); 
                Thread.Sleep(200);//不加入Sleep容易超时
                response = (HttpWebResponse)request.GetResponse(); 
                sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                result = sr.ReadToEnd().Trim();
            }
            catch (System.Threading.ThreadAbortException e)
            { 
                System.Threading.Thread.ResetAbort();
                return result;
            }
            catch (WebException e)
            {  
                return result;
            }
            catch (Exception e)
            { 
                return result;
            }
            finally
            {
                if (reqStream != null)
                {
                    reqStream.Close();
                }
                if (sr != null)
                {
                    sr.Close();
                } 
                if (response != null)
                {
                    response.Close();
                    response = null;
                }
                if (request != null)
                {
                    request.Abort();
                    request = null;
                }
            }
            return result;

 

HTTP POS

标签:ons   utf8   encoding   method   ext   lse   pos   finally   bre   

原文地址:https://www.cnblogs.com/qi123/p/9777403.html

(0)
(0)
   
举报
评论 一句话评论(0
0条  
登录后才能评论!
© 2014 mamicode.com 版权所有 京ICP备13008772号-2
迷上了代码!