zoukankan      html  css  js  c++  java
  • C# 后端调用 Web API

    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;
            public static WebApiHelper Instance
            {
                get
                {
                    if (instance == null) instance = new WebApiHelper();
                    return WebApiHelper.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();
                }
            }
    
    
        }
    }
  • 相关阅读:
    Django—使用后台管理Models
    Django—开发具体流程
    Sqlite—数据库管理与表管理
    Sqlite—数据类型
    Python—实现钉钉后台开发
    Xdebug文档(一)基本特性
    FHS定义的Linux目录树
    【转】给Windows + Apache 2.2 + PHP 5.3 安装PHP性能测试工具 xhprof
    【转】UTF-8汉字正则表达式
    【转】Nginx区分PC或手机访问不同网站
  • 原文地址:https://www.cnblogs.com/YYkun/p/14029955.html
Copyright © 2011-2022 走看看