C# httpwebreuqest post传参数json



public static string GetMobileConfByUserId(string userid)
        {
            string rs = null;
            if (!string.IsNullOrEmpty(userid))
            {
                if (string.IsNullOrEmpty(strOnLine))
                {
                    strOnLine = ConfigurationManager.AppSettings["OnLineUrl"];
                }
                ServicePointManager.DefaultConnectionLimit = 300;
                System.GC.Collect();
                CookieContainer cookieContainer = new CookieContainer();
                // 设置提交的相关参数
                HttpWebRequest request = null;
                HttpWebResponse SendSMSResponse = null;
                Stream dataStream = null;
                StreamReader SendSMSResponseStream = null;
                try
                {
                    request = WebRequest.Create(strOnLine) as HttpWebRequest;
                    request.Method = "POST";
                    request.KeepAlive = false;
                    request.ServicePoint.ConnectionLimit = 300;
                    request.AllowAutoRedirect = true;
                    request.Timeout = 10000;
                    request.ReadWriteTimeout = 10000;
                    request.ContentType = "application/json";
                    request.Accept = "application/xml";
                    request.Headers.Add("X-Auth-Token", HttpUtility.UrlEncode("openstack"));
                    string strContent = "{\"appId\":\"\",\"method\":\"\",\"tokenId\":\"\",\"data\":{\"userAccountId\":\"" + userid + "\"}}";            
                    byte[] bytes = Encoding.UTF8.GetBytes(strContent);                                       
                    request.Proxy = null;
                    request.CookieContainer = cookieContainer;
                    using (dataStream = request.GetRequestStream())
                    {
                        dataStream.Write(bytes, 0, bytes.Length);
                    }          
                    SendSMSResponse = (HttpWebResponse)request.GetResponse();
                    if (SendSMSResponse.StatusCode == HttpStatusCode.RequestTimeout)
                    {
                        if (SendSMSResponse != null)
                        {
                            SendSMSResponse.Close();
                            SendSMSResponse = null;
                        }
                        if (request != null)
                        {
                            request.Abort();
                        }
                        return null;
                    }
                    SendSMSResponseStream = new StreamReader(SendSMSResponse.GetResponseStream(), Encoding.GetEncoding("utf-8"));
                    string strRespone = SendSMSResponseStream.ReadToEnd();
                   
                    return strRespone;
                }
                catch (Exception ex)
                {
                  
                    if (dataStream != null)
                    {
                        dataStream.Close();
                        dataStream.Dispose();
                        dataStream = null;
                    }
                    if (SendSMSResponseStream != null)
                    {
                        SendSMSResponseStream.Close();
                        SendSMSResponseStream.Dispose();
                        SendSMSResponseStream = null;
                    }
                    if (SendSMSResponse != null)
                    {
                        SendSMSResponse.Close();
                        SendSMSResponse = null;
                    }
                    if (request != null)
                    {
                        request.Abort();
                    }
                }
                finally
                {
                    if (dataStream != null)
                    {
                        dataStream.Close();
                        dataStream.Dispose();
                        dataStream = null;
                    }
                    if (SendSMSResponseStream != null)
                    {
                        SendSMSResponseStream.Close();
                        SendSMSResponseStream.Dispose();
                        SendSMSResponseStream = null;
                    }
                    if (SendSMSResponse != null)
                    {
                        SendSMSResponse.Close();
                        SendSMSResponse = null;
                    }
                    if (request != null)
                    {
                        request.Abort();
                    }
                }
            }
            return rs;
        }


相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页