zoukankan      html  css  js  c++  java
  • C# POST/GET

    C# Post/Get 一个demo记录一下。

        class HttpManage
        {
            /// <summary>
            /// POST请求
            /// </summary>
            /// <param name="postURL">请求URL</param>
            /// <param name="jsonParam">请求参数JSON格式</param>
            /// <returns></returns>
            public static string POST(string postURL, string jsonParam)
            {
                System.Net.ServicePointManager.DefaultConnectionLimit = 50;
    
                string result = string.Empty;
                string serviceAddress = postURL;
    
                HttpWebRequest request = null;
                try
                {
                    request = (HttpWebRequest)WebRequest.Create(serviceAddress);
                    request.Method = "POST";
                    request.ContentType = "application/json";
                    //request.ContentType = " application/x-www-form-urlencoded";   //内容类型
                    request.ReadWriteTimeout = 30 * 1000;
    
                    string strContent = jsonParam;
                    //using (StreamWriter dataStream = new StreamWriter(request.GetRequestStream()))
                    //{
                    //    dataStream.Write(strContent);
                    //    dataStream.Close();
                    //}
                    byte[] data = Encoding.UTF8.GetBytes(strContent);
                    Stream myRequestStream = request.GetRequestStream();
                    myRequestStream.Write(data, 0, data.Length);
                    myRequestStream.Close();
    
                    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                    {
                        string encoding = response.ContentEncoding;
                        if (encoding == null || encoding.Length < 1)
                        {
                            encoding = "UTF-8"; //默认编码  
                        }
                        StreamReader reader = null;
                        reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
                        result = reader.ReadToEnd();
                        reader.Close();
                    }
                }
                catch (Exception ex)
                {
                    //异常,写日志
                    Log4NetHelper.WriteErrorLog("POST异常",ex);
                }
                finally
                {
                    if (request != null)
                    {
                        request.Abort();
                    }
                }
                return result;
            }
    
            public static string PostFromHttpClient(string url,string jsonParam)
            {
                string result = string.Empty;
                return result;
            }
    
            /// <summary>
            /// GET请求
            /// </summary>
            /// <param name="url">请求URL</param>
            /// <returns></returns>
            public static string GET(string url)
            {
                string result = string.Empty;
    
                HttpWebRequest request = null;
                try
                {
                    request = WebRequest.Create(url) as HttpWebRequest;  //根据用户请求的RUL创建WEB访问对象
                    //定义请求方法和标识
                    request.Method = "GET";
                    request.ContentType = "application/json";
                    //request.ContentType = "text/html;charset=UTF-8";
    //request.ContentType = " application/x-www-form-urlencoded"; //接收请求 using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) //获取响应请求 { //以流的形式读取数据 StreamReader sr = new StreamReader(response.GetResponseStream()); result = sr.ReadToEnd(); sr.Close(); } } catch(Exception ex) { result = string.Empty; Log4NetHelper.WriteErrorLog("GET异常",ex); } finally { if (request != null) { request.Abort(); } } return result; }

    欢迎相互交流学习!

  • 相关阅读:
    DDD 领域驱动设计-谈谈 Repository、IUnitOfWork 和 IDbContext 的实践
    UVA10071 Back to High School Physics
    UVA10071 Back to High School Physics
    UVA10055 Hashmat the Brave Warrior
    UVA10055 Hashmat the Brave Warrior
    UVA458 The Decoder
    UVA458 The Decoder
    HDU2054 A == B ?
    HDU2054 A == B ?
    POJ3414 Pots
  • 原文地址:https://www.cnblogs.com/jiayan1578/p/11903879.html
Copyright © 2011-2022 走看看