zoukankan      html  css  js  c++  java
  • Web API 学习笔记3

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Text;
    
    namespace ZB.QueueSys.Common
    {
        public class WebApiHelper
        {
            //定义一个用于保存静态变量的实例
            private static WebApiHelper instance = null;
            //定义一个保证线程同步的标识
            private static readonly object locker = new object();
            //构造函数为私有,使外界不能创建该类的实例
            private WebApiHelper() { }
            public static WebApiHelper Instance
            {
                get
                {
                    if (instance == null)
                    {
                        lock (locker)
                        {
                            if (instance == null) instance = new WebApiHelper();
                        }
                    }
                    return instance;
                }
            }
    
            public string HttpGet(string url)
            {
                //string ss = HttpGet("http://localhost:000/api/Demo/GetXXX?Name=北京");
                //ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                Encoding encoding = Encoding.UTF8;
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = "GET";
                request.Accept = "text/html, application/xhtml+xml, */*";
                request.ContentType = "application/json";
    
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                {
                    string str = reader.ReadToEnd();
                    return str;
                }
            }
    
            public string HttpPost(string url, string body)
            {
                //string ss = HttpPost("http://localhost:000/api/Demo/PostXXX", "{Code:"test089",Name:"test1"}");
                //ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                Encoding encoding = Encoding.UTF8;
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = "POST";
                request.Accept = "text/html, application/xhtml+xml, */*";
                request.ContentType = "application/json";
    
                byte[] buffer = encoding.GetBytes(body);
                request.ContentLength = buffer.Length;
                request.GetRequestStream().Write(buffer, 0, buffer.Length);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                {
                    return reader.ReadToEnd();
                }
            }
    
    
        }
    }
    

      

    博客内容主要用于日常学习记录,内容比较随意,如有问题,还需谅解!!!
  • 相关阅读:
    Check the string
    最简单的
    第七届ACM程序设计竞赛 (SDIBT)
    Cutie Pie
    CSS3 :nth-of-type() 与 nth-child()选择器
    Ajax 完整教程 转载地址:http://www.cnblogs.com/Garden-blog/archive/2011/03/11/1981778.html(转)
    排序:二元选择排序
    排序:堆排序
    排序:直接插入排序 稳定 n*n
    排序:冒泡和改进
  • 原文地址:https://www.cnblogs.com/YYkun/p/14549841.html
Copyright © 2011-2022 走看看