zoukankan      html  css  js  c++  java
  • C# Http Get 提交请求

            /// <summary>
            /// HTTP GET方式请求数据.
            /// </summary>
            /// <param name="url">URL.</param>
            /// <returns></returns>
            public static string HttpGet(string url)
            {
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
                request.Method = "GET";
                //request.ContentType = "application/x-www-form-urlencoded";
                request.Accept = "*/*";
                request.Timeout = 15000;
                request.AllowAutoRedirect = false;
    
                WebResponse response = null;
                string responseStr = null;
    
                try
                {
                    response = request.GetResponse();
    
                    if (response != null)
                    {
                        StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                        responseStr = reader.ReadToEnd();
                        reader.Close();
                    }
                }
                catch (Exception)
                {
                    throw;
                }
                finally
                {
                    request = null;
                    response = null;
                }
    
                return responseStr;
            }

            /// <summary>
            /// HTTP POST方式请求数据.
            /// </summary>
            /// <param name="url">URL.</param>
            /// <returns></returns>
            public static string HttpPost(string Url, string postDataStr)
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                request.ContentLength = Encoding.UTF8.GetByteCount(postDataStr);
               // request.CookieContainer = cookie;
                Stream myRequestStream = request.GetRequestStream();
                StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
                myStreamWriter.Write(postDataStr);
                myStreamWriter.Close();
    
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    
                //response.Cookies = cookie.GetCookies(response.ResponseUri);
                Stream myResponseStream = response.GetResponseStream();
                StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
                string retString = myStreamReader.ReadToEnd();
                myStreamReader.Close();
                myResponseStream.Close();
    
                return retString;
            }
    
    
    
     
  • 相关阅读:
    实习项目1.
    try catch finally的用法
    asp.net 验证控件
    数据库操作语言
    webform Response的一些成员
    jQuery
    C#@的用法
    SQL分页查询
    抽象类与接口的区别
    抽象类与接口
  • 原文地址:https://www.cnblogs.com/2013likong/p/5585675.html
Copyright © 2011-2022 走看看