C# 后台获取API接口数据
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接口数据的更多相关文章
- 如何通过图片在 HTTPS 网站中获取 HTTP 接口数据
<script> (function() { var Decode=function(b){var e;e=[];var a=b.width,c=b.height,d=document.c ...
- PHP--通用化API接口数据输出 封装
/** * 通用化API接口数据输出 * author qinpeizhou * @param $message * @param array $data * @param int $httpCode ...
- springboot框架中集成thymeleaf引擎,使用form表单提交数据,debug结果后台获取不到数据
springboot框架中集成thymeleaf引擎,使用form表单提交数据,debug结果后台获取不到数据 表单html: <form class="form-horizontal ...
- React 获取服务器API接口数据:axios、fetchJsonp
使用axios.fetchJsonp获取服务器的接口数据.其中fetchJsonp是跨域访问 一.使用axios 1.安装axios模块 npm install --save axios 2.引用模块 ...
- (二)校园信息通微信小程序从后台获取首页的数据笔记
在从后台获取数据之前,需要先搭建好本地服务器的环境. 确保Apache,MySql处于开启状态.下图为Apache,MySql处于开启时状态 然后进入后台管理平台进行字段和列表的定义 然后在后台添加数 ...
- form enctype:"multipart/form-data",method:"post" 提交表单,后台获取不到数据
在解决博问node.js接受参数的时候,发现当form中添加enctype:"multipart/form-data",后台确实获取不到数据,于是跑到百度上查了一下,终于明白为什么 ...
- Servlet的5种方式实现表单提交(注册小功能),后台获取表单数据
用servlet实现一个注册的小功能 ,后台获取数据. 注册页面: 注册页面代码 : <!DOCTYPE html> <html> <head> <meta ...
- AngularJs $http.post 数据后台获取不到数据问题 的解决过程
第一次使用 AngularJs 的 $http 模块的时候,遇到过后台获取不到前台提交数据的问题,检查代码没有发现问题,先上代码. js 代码 angular.module("newsApp ...
- 微信小程序入门教程(一)API接口数据记录
今天测试用小程序调用API接口,发现有些数据打印都是对象,怎么全部打印详细点来 小程序代码: httpsearch: function (name, offset, type, cb) { wx.re ...
随机推荐
- PAT-GPLT训练集 L1-043 阅览室
PAT-GPLT训练集 L1-043 阅览室 注意:连续的S和E才算一次借还 代码: #include<iostream> #include<cstdio> using nam ...
- Win10系列:UWP界面布局基础5
(2)编写后台代码访问资源 下面通过一个例子来演示如何编写后台代码引用资源.新建一个Windows应用商店的空白应用程序项目,将其命名为AccessResourceApplication,打开项目下的 ...
- python 类方法中参数使用默认值的方法
class A(): __init__(self, **arg): self.__dict__.update(arg) def M(self, config=None, section= ...
- nginx入门示例(二)
nginx使用域名访问 (Tip) nginx目录解析 conf/nginx.conf #主要的配置文件目录 html #nginx的默认发布目录,部署完后 网站会在这个目录 ...
- Win10访问不到XP共享的解决:
不知道别人的是怎么解决. 反正我这么解决了. 我的win10笔记本,是使用windows帐户登陆的.可以同步很多东西. 同事的电脑是台式老古董XP. 扫描不到网上邻居,手动\\ip也访问不到. 最后安 ...
- js 动态绑定鼠标事件
<script> function getElementsByClassName(n) { var classElements = [],allElements = document.ge ...
- MFC 中GetClientRect、ClientToScreen、GetWindow、RectScreenToClient的使用
CWnd* pWnd = GetDlgItem(IDB_BUT_RECOGNIZE); pWnd->GetClientRect(&rect); //指该控件自身客户区的矩形,原点为控 ...
- SharePoint REST API - 基本操作(一)
博客地址:http://blog.csdn.net/FoxDave 本文讲述如何应用SharePoint的REST接口完成基本的增删查改操作. 使用SharePoint客户端API和REST服务进 ...
- POJ - 3252 A - Round Numbers
The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, Paper, Stone' ...
- Linux可重入函数和线程安全的区别与联系(转)
*****可重入函数 函数被不同的控制流程调用,有可能在第一次调用还没返回时就再次进入该函数,这称为重入. 当程序运行到某一个函数的时候,可能因为硬件中断或者异常而使得在用户正在执行的代码暂时终端转而 ...