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. python小数据池,代码块知识

    一.什么是代码块? 根据官网提示我们可以获知: A Python program is constructed from code blocks. A block is a piece of Pyth ...

  2. ES6中的Promise.resolve()的作用

    var foo = { then: (resolve, reject) => resolve('foo') }; var resolved = Promise.resolve(foo); 相当于 ...

  3. spring bean 的生命周期

    感谢博友,内容源于博友的文章 http://www.cnblogs.com/zrtqsk/p/3735273.html 通过了解spring的bean 的生命周期 ,再结合jdk的注解,继承sprin ...

  4. SpringBoot + JPA 连接MySQL完整实例(一)

    开发工具 1.Eclipse 2.Maven 3.Spring Boot 首先,Eclipse中配置好maven,具体请百度 工程结构: 实现步骤: 1.Eclipse中新建一个maven proje ...

  5. HashMap实现原理分析--面试详谈

    1. HashMap的数据结构 数据结构中有数组和链表来实现对数据的存储,但这两者基本上是两个极端. 数组 数组存储区间是连续的,占用内存严重,故空间复杂的很大.但数组的二分查找时间复杂度小,为O(1 ...

  6. idea自动生成serialVersionUID(转)

    原文链接:http://blog.sina.com.cn/s/blog_54b09dc90101d9bu.html Setting->Plugins 找到一个叫  GenerateSerialV ...

  7. ESP8266 上报数据到贝壳物联

    下面贴上代码: #include <aJSON.h> #include <SPI.h> #include <Wire.h> #include <dht11.h ...

  8. arduino使用oled显示时间MQ_2温湿度

    这代码一般都是复制过来,在小改下就行了 代码如下: double Fahrenheit(double celsius) { ; } //摄氏温度度转化为华氏温度 double Kelvin(doubl ...

  9. Kafka实践

    1. kafka发送方法 @Component@Import(KafkaAutoProperties.class)public class KafkaProducer { @Autowired pri ...

  10. 如何使用Android Studio把自己的Android library分享到jCenter和Maven Central

    参考链接: http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0623/3097.html