using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks; namespace ClientTest
{
class BasicClient
{
public string SendRequest(string url, string method, string auth, string reqParams)
{
//这是发送Http请求的函数,可根据自己内部的写法改造
HttpWebRequest myReq = null;
HttpWebResponse response = null;
string result = string.Empty;
try
{
myReq = (HttpWebRequest)WebRequest.Create(url);
myReq.Method = method;
myReq.ContentType = "application/json;";
myReq.KeepAlive = false; //basic 验证下面这句话不能少
if (!String.IsNullOrEmpty(auth))
{
myReq.Headers.Add("Authorization", "Basic " + auth);
} if (method == "POST" || method == "PUT")
{
byte[] bs = Encoding.UTF8.GetBytes(reqParams);
myReq.ContentLength = bs.Length;
using (Stream reqStream = myReq.GetRequestStream())
{
reqStream.Write(bs, , bs.Length);
reqStream.Close();
}
} response = (HttpWebResponse)myReq.GetResponse();
HttpStatusCode statusCode = response.StatusCode;
if (Equals(response.StatusCode, HttpStatusCode.OK))
{
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
result = reader.ReadToEnd();
}
}
}
catch (WebException e)
{
if (e.Status == WebExceptionStatus.ProtocolError)
{
HttpStatusCode errorCode = ((HttpWebResponse)e.Response).StatusCode;
string statusDescription = ((HttpWebResponse)e.Response).StatusDescription;
using (StreamReader sr = new StreamReader(((HttpWebResponse)e.Response).GetResponseStream(), Encoding.UTF8))
{
result = sr.ReadToEnd();
}
}
else
{
result = e.Message;
}
}
finally
{
if (response != null)
{
response.Close();
}
if (myReq != null)
{
myReq.Abort();
}
} return result;
} public string sendHttpRequest(string url, string reqparam, string method = "POST")
{
string auth = Base64Encode("key:secret");
return SendRequest(url, method, auth, reqparam);
} private string Base64Encode(string value)
{
byte[] bytes = Encoding.Default.GetBytes(value);
return Convert.ToBase64String(bytes);
}
}
}

C# 后台获取API接口数据的更多相关文章

  1. 如何通过图片在 HTTPS 网站中获取 HTTP 接口数据

    <script> (function() { var Decode=function(b){var e;e=[];var a=b.width,c=b.height,d=document.c ...

  2. PHP--通用化API接口数据输出 封装

    /** * 通用化API接口数据输出 * author qinpeizhou * @param $message * @param array $data * @param int $httpCode ...

  3. springboot框架中集成thymeleaf引擎,使用form表单提交数据,debug结果后台获取不到数据

    springboot框架中集成thymeleaf引擎,使用form表单提交数据,debug结果后台获取不到数据 表单html: <form class="form-horizontal ...

  4. React 获取服务器API接口数据:axios、fetchJsonp

    使用axios.fetchJsonp获取服务器的接口数据.其中fetchJsonp是跨域访问 一.使用axios 1.安装axios模块 npm install --save axios 2.引用模块 ...

  5. (二)校园信息通微信小程序从后台获取首页的数据笔记

    在从后台获取数据之前,需要先搭建好本地服务器的环境. 确保Apache,MySql处于开启状态.下图为Apache,MySql处于开启时状态 然后进入后台管理平台进行字段和列表的定义 然后在后台添加数 ...

  6. form enctype:"multipart/form-data",method:"post" 提交表单,后台获取不到数据

    在解决博问node.js接受参数的时候,发现当form中添加enctype:"multipart/form-data",后台确实获取不到数据,于是跑到百度上查了一下,终于明白为什么 ...

  7. Servlet的5种方式实现表单提交(注册小功能),后台获取表单数据

    用servlet实现一个注册的小功能 ,后台获取数据. 注册页面: 注册页面代码 : <!DOCTYPE html> <html> <head> <meta ...

  8. AngularJs $http.post 数据后台获取不到数据问题 的解决过程

    第一次使用 AngularJs 的 $http 模块的时候,遇到过后台获取不到前台提交数据的问题,检查代码没有发现问题,先上代码. js 代码 angular.module("newsApp ...

  9. 微信小程序入门教程(一)API接口数据记录

    今天测试用小程序调用API接口,发现有些数据打印都是对象,怎么全部打印详细点来 小程序代码: httpsearch: function (name, offset, type, cb) { wx.re ...

随机推荐

  1. linux的典型分支:

    1.redhat 2.debian 3.centOS 4.ubuntu 5.fedora 6.kali linux

  2. .NetCore发布到Centos docker

    将.netcore mvc项目发布到centos7的docker中.环境 vmware14+Centos7+docker-ce 1.使用vs将.netcoremvc项目发布到本地,修改发布后的目录 名 ...

  3. CCF关于对NOIP2018复赛违规处罚的公告

    NOIP2018复赛于11月10-11日在全国31个赛区同时举行,现已结束.总体有序,但也有赛区出现违规现象.现将复赛中违规情况进行通报. 一.数据提交情况 CCF要求NOI各省组织单位在考试结束后在 ...

  4. application Initialization设置导致处理程序ExtensionlessUrlHandler-Integrated-4.0在其模块列表中有一个错误模块问题的解决

    HTTP 错误 500.21 - Internal Server Error 处理程序“ExtensionlessUrlHandler-Integrated-4.0”在其模块列表中有一个错误模块“Ma ...

  5. Asp.Net 中 HTTP 和 HTTPS 切换

    Asp.Net 中 HTTP 和 HTTPS 切换   目的 HTTP,超文本传输协议,明文传输,无状态,服务器默认端口80 HTTPS,具有SSL加密的HTTP,加密传输,需要申请ca证书,服务器默 ...

  6. 用老毛桃U盘安装:[3]Ghost版Win7系统

    用老毛桃自动安装Ghost版Win7的步骤: 1,到网上先下载Ghost版Win7映像文件到硬盘,我放到的是U盘,盘符为Z,如果你愿意,可直接放到硬盘即可,放到硬盘安装速度会快一点. 2,把制作好的老 ...

  7. bzoj4237

    题解: cdq分治 二位变成一维 二分一下 代码: #include<bits/stdc++.h> using namespace std; typedef long long LL; ; ...

  8. [javamail]AUTH LOGIN failed;Invalid username or password报错

    项目中需要用到javamailAPI,邮箱服务器用的sohu闪电邮,SMTP协议用来发送,赋值代码: Properties props = new Properties(); props.setPro ...

  9. 3.Python爬虫入门三之Urllib和Urllib2库的基本使用

    1.分分钟扒一个网页下来 怎样扒网页呢?其实就是根据URL来获取它的网页信息,虽然我们在浏览器中看到的是一幅幅优美的画面,但是其实是由浏览器解释才呈现出来的,实质它是一段HTML代码,加 JS.CSS ...

  10. Spring学习二

    Spring的表达式语言 SpEL,使用方法是#{} SpEL的特性如下 使用Bean 的ID 来引用Bean : 调用方法和访问对象的属性: 对值进行算术.关系和逻辑运算: 正则表达式匹配: 集合操 ...