zoukankan      html  css  js  c++  java
  • 创建并发送请求,带xml

    private static string NcPost(string url, string postCont, int timeOut, bool sign)
            {
                Encoding encoding = Encoding.GetEncoding("gb2312");
                byte[] bytesToPost = encoding.GetBytes(postCont);
                string cookieheader = string.Empty;
    
                var cookieCon = new CookieContainer();
    
                #region 创建HttpWebRequest对象
    
                var httpRequest = (HttpWebRequest)WebRequest.Create(url);
    
                #endregion
    
                #region 初始化HtppWebRequest对象
    
                httpRequest.CookieContainer = cookieCon;
                httpRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";
                httpRequest.ContentType = "application/x-www-form-urlencoded";
                httpRequest.Method = "POST";
                httpRequest.Timeout = timeOut * 1000;
                if (sign)
                {
                    httpRequest.ContentType = "INFOSEC_SIGN/1.0";
                    httpRequest.ContentLength = bytesToPost.Length;
                }
    
    
                if (cookieheader.Equals(string.Empty))
                {
                    httpRequest.CookieContainer.GetCookieHeader(new Uri(url));
                }
                else
                {
                    httpRequest.CookieContainer.SetCookies(new Uri(url), cookieheader);
                }
    
                #endregion
    
                string stringResponse = "";
                try
                {
    
                    #region 附加Post给服务器的数据到HttpWebRequest对象
    
                    httpRequest.ContentLength = bytesToPost.Length;
                    Stream requestStream = httpRequest.GetRequestStream();
                    requestStream.Write(bytesToPost, 0, bytesToPost.Length);
                    requestStream.Close();
    
                    #endregion
    
    
                    #region 读取服务器返回信息
    
    
                    Stream responseStream = httpRequest.GetResponse().GetResponseStream();
    
                    if (responseStream != null)
                    {
                        using (
                            var responseReader = new StreamReader(responseStream, Encoding.GetEncoding("gbk")))
                        {
                            stringResponse = responseReader.ReadToEnd();
                        }
                        responseStream.Close();
                    }
    
                    #endregion
                }
                catch (Exception ex)
                {
                    stringResponse = ex.ToString();
                }
                return stringResponse;
            }
  • 相关阅读:
    JavaScript实现文本框和密码框placeholder效果(兼容ie8)
    11.24 模拟赛题解
    一句话题解集——口胡万岁
    uTools-插件化定制属于自己的工具集[免费]
    tree
    braintree 支付
    Shopify 接口调用
    TcPlayer腾讯播放器
    微信支付(WeixinJSBridge.invoke、wx.chooseWXPay)
    图片上传(二进制文件流)
  • 原文地址:https://www.cnblogs.com/ysf123/p/4086431.html
Copyright © 2011-2022 走看看