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();
                }
            }
    
    
        }
    }
    

      

    博客内容主要用于日常学习记录,内容比较随意,如有问题,还需谅解!!!
  • 相关阅读:
    数据分析 --- 01. Numpy
    爬虫 --- 08. 全站爬取(CrawlSpider), 分布式, 增量式爬虫
    爬虫 --- 07. 全站爬取(手动), post请求,cookie, 传参,中间件,selenium
    数据结构 --- 02. 内存, 顺序表, 单链表
    数据结构 --- 01. 时间复杂度,timeit模块,栈,队列,双端队列
    爬虫 --- 06. scrapy框架初始,移动端数据爬取
    爬虫 --- 05. 异步协程, 浏览器自动化,
    爬虫 --- 04. 代理服务器, 验证码识别, 处理cookie,线程池
    爬虫 --- 02. 爬取图片,数据解析
    一个关于const 变量作为map键值的Bug
  • 原文地址:https://www.cnblogs.com/YYkun/p/14549841.html
Copyright © 2011-2022 走看看