zoukankan      html  css  js  c++  java
  • 如何调用Http请求的接口

            /// <summary>
            /// 发起一个HTTP请求(以POST方式)
            /// </summary>
            /// <param name="url"></param>
            /// <param name="param"></param>
            /// <returns></returns>
            public static string HttpPost(string url, string param = "")
            {
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                request.Accept = "*/*";
                request.Timeout = 10000;
                request.AllowAutoRedirect = false;
                StreamWriter requestStream = null;
                WebResponse response = null;
                string responseStr = null;
                try
                {
                    requestStream = new StreamWriter(request.GetRequestStream());
                    requestStream.Write(param);
                    requestStream.Close();
                    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;
                    requestStream = null;
                    response = null;
                }
                return responseStr;
            }
     
    /// <summary>
            /// 发起一个HTTP请求(以GET方式)
            /// </summary>
            /// <param name="url"></param>
            /// <returns></returns>
            public static string HttpGet(string url)
            {
                WebRequest myWebRequest = WebRequest.Create(url); 
                WebResponse myWebResponse = myWebRequest.GetResponse(); 
                Stream ReceiveStream = myWebResponse.GetResponseStream(); 
                string responseStr = "";
                if (ReceiveStream != null)
                {
                    StreamReader reader = new StreamReader(ReceiveStream, Encoding.UTF8);
                    responseStr = reader.ReadToEnd();
                    reader.Close(); 
                }
                myWebResponse.Close();
                return responseStr;
            }

    转:http://bbs.csdn.net/topics/391004728

  • 相关阅读:
    平安夜前夜,我在上海沐恩堂度过
    吃匹萨的数学
    2004语录
    爱的罗曼斯
    转发: 上季度全球最佳短篇小说《最后一趟生意》
    一个人的元宵节
    超越纪念我的blog排名上升到第六位
    读《仆人》
    我的龟宝宝病了:( 谁来救救她?
    春晚
  • 原文地址:https://www.cnblogs.com/wangfuyou/p/5961423.html
Copyright © 2011-2022 走看看