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 ...
随机推荐
- (C/C++学习笔记) 二十三. 运行时类型识别
二十三. 运行时类型识别 ● 定义 运行时类型识别(Run-time Type Identification, RTTI) 通过RTTI, 程序能够使用基类的指针或引用来检查(check)这些指针或引 ...
- Android : alsa-lib 移植
一.官网下载lib源码 网址:http://www.alsa-project.org/main/index.php/Download#alsa-lib 左击:Stable Release列表中的[1. ...
- 3.5 C++间接继承
参考:http://www.weixueyuan.net/view/6362.html 总结: 假设类C继承自类B,类B继承自类A.那么类C中的除了能够继承B类的成员函数和成员变量外,同样也能继承B类 ...
- Spring框架的四大原则
Spring框架本身有四大原则: 1).使用POJO进行轻量级和最小入侵式开发 2).通过以来注入和基于接口编程实现松耦合 3).通过AOP和默认习惯进行声明式编程 4).使用AOP和模板减少模式化代 ...
- DevExpress ASP.NET Core Controls v18.2新功能详解
行业领先的.NET界面控件2018年第二次重大更新——DevExpress v18.2日前正式发布,本站将以连载的形式为大家介绍新版本新功能.本文将介绍了DevExpress ASP.NET Core ...
- 查看电脑的IP地址及配置
自己主机的IP地址查看cmd----ipconfig/all,如下图
- Java输入输出小结
无论使用哪一种编程语言,输入输出都是我们首当其冲的,因此简单整理了 一下关于Java输入输出知识点,还有些内容摘自其它博客,忘见谅. 第一部分,让我们看一下Java的输出 public class M ...
- mybatis 异常 There is no getter for property named 'bizId' in 'class java.lang.Long'
mybatis 异常 There is no getter for property named 'bizId' in 'class java.lang.Long' 当使用mybatis进行传参的时候 ...
- 从SharePoint 2013迁移到SharePoint Online - 评估工具
博客地址:http://blog.csdn.net/FoxDave 今天想跟大家分享一款从SharePoint 2013迁移到SharePoint Online时的评估工具:SharePoint ...
- 秦皇岛CCPC的失败总结
个人状态原因:尤其是我,在比赛前没有很好的做准备,还一直看小说,前两天我们本来应该好好打两场训练赛的时候却没有打,然后一直在玩手机,比赛前一天,我下午就不小心睡着了,然后晚上醒来睡不着第二天的精神状态 ...