using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks; namespace Edc.Dao
{
public class HttpAPI
{
private const string DefaultUserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36";
public string Compleatehtml = "";
private void BugFix_CookieDomain(CookieContainer cookieContainer)
{
System.Type _ContainerType = typeof(CookieContainer);
Hashtable table = (Hashtable)_ContainerType.InvokeMember("m_domainTable",
System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.GetField |
System.Reflection.BindingFlags.Instance,
null,
cookieContainer,
new object[] { });
ArrayList keys = new ArrayList(table.Keys);
foreach (string keyObj in keys)
{
string key = (keyObj as string);
if (key[0] == '.')
{
string newKey = key.Remove(0, 1);
table[newKey] = table[keyObj];
}
}
} public String DoGet(String url)
{
String html = "";
StreamReader reader = null;
HttpWebRequest webReqst = (HttpWebRequest)WebRequest.Create(url);
webReqst.Method = "GET";
webReqst.UserAgent = DefaultUserAgent;
webReqst.KeepAlive = true;
webReqst.CookieContainer = new CookieContainer();
webReqst.Timeout = 30000;
webReqst.ReadWriteTimeout = 30000;
try
{
HttpWebResponse webResponse = (HttpWebResponse)webReqst.GetResponse();
BugFix_CookieDomain(webReqst.CookieContainer);
if (webResponse.StatusCode == HttpStatusCode.OK)//&& webResponse.ContentLength < 1024 * 1024
{
Stream stream = webResponse.GetResponseStream();
stream.ReadTimeout = 30000;
if (webResponse.ContentEncoding == "gzip")
{
reader = new StreamReader(new GZipStream(stream, CompressionMode.Decompress), Encoding.Default);
}
else
{
reader = new StreamReader(stream, Encoding.UTF8);
}
html = reader.ReadToEnd();
}
}
catch (Exception ex)
{
throw ex;
} return html;
} public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{
return true; //总是接受
} public void DoPost(String url, String Content)
{
HttpWebRequest webReqst = null;
//如果是发送HTTPS请求
if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
{
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
webReqst = WebRequest.Create(url) as HttpWebRequest;
webReqst.ProtocolVersion = HttpVersion.Version10;
}
else
{
webReqst = WebRequest.Create(url) as HttpWebRequest;
}
webReqst.Method = "POST";
webReqst.UserAgent = DefaultUserAgent;
webReqst.ContentType = "application/x-www-form-urlencoded";
webReqst.ContentLength = Content.Length;
webReqst.CookieContainer = new CookieContainer();
webReqst.Timeout = 30000;
webReqst.ReadWriteTimeout = 30000;
try
{
//System.Text.Encoding encoding = System.Text.Encoding.GetEncoding("gb2312");
byte[] data = Encoding.UTF8.GetBytes(Content);
webReqst.ContentLength = data.Length;
Stream stream = webReqst.GetRequestStream();
stream.Write(data, 0, data.Length); webReqst.BeginGetRequestStream(new AsyncCallback(Compleate), webReqst);
}
catch (Exception ex)
{
throw ex;
}
} private void Compleate(IAsyncResult asyncResult)
{
//Console.WriteLine("异步完成");
if (asyncResult == null)
{
return;
}
HttpWebRequest req = (asyncResult.AsyncState as HttpWebRequest);
HttpWebResponse res = req.GetResponse() as HttpWebResponse;
StreamReader reader = new StreamReader(res.GetResponseStream());
var html = reader.ReadToEnd();
//Console.WriteLine(reader.ReadToEnd());
Compleatehtml = html;
} public static string str;
public static HttpWebRequest request;
public string DoDelete(String url)
{
string urlPath = url;
//urlPath = urlPath + id;
int millisecond = 30000;
WebResponse response = null;
StreamReader reader = null;
try
{
request = (HttpWebRequest)WebRequest.Create(urlPath);
//request.Proxy = null;//关闭代理(重要)
request.Timeout = millisecond;
request.Method = "DELETE";
//request.Accept = "application/json";
//request.ContentType = "application/json";
request.ServicePoint.Expect100Continue = false;
response = (WebResponse)request.GetResponse();
reader = new StreamReader(response.GetResponseStream());
str = reader.ReadToEnd();
}
catch (Exception ex)
{ str = "";
}
return str;
}
public void DoPut(String url, String Content)
{ HttpWebRequest webReqst = null;
//如果是发送HTTPS请求
if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
{
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
webReqst = WebRequest.Create(url) as HttpWebRequest;
webReqst.ProtocolVersion = HttpVersion.Version10;
}
else
{
webReqst = WebRequest.Create(url) as HttpWebRequest;
}
webReqst.Method = "PUT";
webReqst.UserAgent = DefaultUserAgent;
webReqst.ContentType = "application/x-www-form-urlencoded";
webReqst.ContentLength = Content.Length;
webReqst.CookieContainer = new CookieContainer();
webReqst.Timeout = 30000;
webReqst.ReadWriteTimeout = 30000;
try
{
//System.Text.Encoding encoding = System.Text.Encoding.GetEncoding("gb2312");
byte[] data = Encoding.UTF8.GetBytes(Content);
webReqst.ContentLength = data.Length;
Stream stream = webReqst.GetRequestStream();
stream.Write(data, 0, data.Length); webReqst.BeginGetRequestStream(new AsyncCallback(Compleate), webReqst);
}
catch
{ }
} }
}

  

Web API后端调用接口 (Get,POST,Put,Delete)的更多相关文章

  1. 从零开始学习 asp.net core 2.1 web api 后端api基础框架(二)-创建项目

    原文:从零开始学习 asp.net core 2.1 web api 后端api基础框架(二)-创建项目 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.ne ...

  2. Web API系列(二)接口安全和参数校验

    以前简单介绍过web api 的设计,但是还是有很多朋友问我,如何合理的设计和实现web api.比如,接口安全,异常处理,统一数据返回等问题.所以有必要系统的总结总结 web api 的设计和实现. ...

  3. Web Api 与 Andriod 接口对接开发经验

    最近一直急着在负责弄Asp.Net Web Api 与 Andriod 接口开发的对接工作! 刚听说要用Asp.Net Web Api去跟 Andriod 那端做接口对接工作,自己也是第一次接触Web ...

  4. Asp.Net Web Api 与 Andriod 接口对接开发经验,给小伙伴分享一下!

    最近一直急着在负责弄Asp.Net Web Api 与 Andriod 接口开发的对接工作! 刚听说要用Asp.Net Web Api去跟 Andriod 那端做接口对接工作,自己也是第一次接触Web ...

  5. Asp.Net Web Api 与 Andriod 接口对接开发

    Asp.Net Web Api 与 Andriod 接口对接开发经验,给小伙伴分享一下!   最近一直急着在负责弄Asp.Net Web Api 与 Andriod 接口开发的对接工作! 刚听说要用A ...

  6. Redis总结(五)缓存雪崩和缓存穿透等问题 Web API系列(三)统一异常处理 C#总结(一)AutoResetEvent的使用介绍(用AutoResetEvent实现同步) C#总结(二)事件Event 介绍总结 C#总结(三)DataGridView增加全选列 Web API系列(二)接口安全和参数校验 RabbitMQ学习系列(六): RabbitMQ 高可用集群

    Redis总结(五)缓存雪崩和缓存穿透等问题   前面讲过一些redis 缓存的使用和数据持久化.感兴趣的朋友可以看看之前的文章,http://www.cnblogs.com/zhangweizhon ...

  7. 从零开始学习 asp.net core 2.1 web api 后端api基础框架(三)-创建Data Transfer Object

    原文:从零开始学习 asp.net core 2.1 web api 后端api基础框架(三)-创建Data Transfer Object 版权声明:本文为博主原创文章,未经博主允许不得转载. ht ...

  8. 从零开始学习 asp.net core 2.1 web api 后端api基础框架(四)-创建Controller

    原文:从零开始学习 asp.net core 2.1 web api 后端api基础框架(四)-创建Controller 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog ...

  9. 从零开始学习 asp.net core 2.1 web api 后端api基础框架(一)-环境介绍

    原文:从零开始学习 asp.net core 2.1 web api 后端api基础框架(一)-环境介绍 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.ne ...

随机推荐

  1. Windows Phone 十六、HttpClient

    HttpClient 对象也可以实现网络请求 相对于 HttpWebRequest 对象来说,HttpClient 操作更简单,功能更强大 HttpClient 提供一系列比较简单的API来实现基本的 ...

  2. Python中的传值和引用

    我写这个主要是给自己看,内容也就是便于自己理解,可能会不正确,但目前来看代码测试的结果是对的. python中一切皆对象. 当我们赋值时: a = 1 其实是先创建了一个整数常量1(也是一个对象,且已 ...

  3. Web 播放声音(AMR 、WAVE)

    最近甚是苦闷,属于边学边做,跳进了很多坑,别提有多惨了,不过结果还是不错滴,纵观前后,一句话足以概括 “痛并快乐着” ~~~ ok,我少说废话,下面来总结下 Web 播放声音一些注意事项. 说到 We ...

  4. SQL Server 2008创建oracle链接服务器(心得)

    操作系统是32位的情况下,曾经没费太多时间创建好了到oracle的链接服务器.主要要点就是: 1.安装oracle精简客户端.当时我用的是版本比较低的“oracle9i310-客户端简化版”,安装好了 ...

  5. C语言 03 项目团队文件合并

    团体项目中 链接把项目中所有相关联的.O目标文件.C语言函数库合并在一起,生成可执行文件. 编写声明文件,用 .h文件封装起来,在其他代码中用include"xxx.h"引用声明 ...

  6. 给linode 替换操作系统核心

    1.Make sure your package repositories and installed packages are up to date by issuing the following ...

  7. HTML、JavaScript之单双引号转义

    一.HTML : 双引号:" 单引号:&apos; 二.JavaScript: 双引号:\" 单引号:\'

  8. 为何iPhone6 Plus的逻辑分辨率是2208×1242,屏幕实际分辨率却是1920×1080

    因为除了iPhone 6+以外,其他所有iPhone的DPI是一致的,都是326,用@2x的素材.但是6+的实际DPI是401,理论上苹果应该用401/326 * @2x=@2.46x的素材,但是这个 ...

  9. Mysql 5.7 Linux安装详细步骤

    版权声明:本文为博主原创文章,未经博主允许不得转载. 1.下载安装包 http://dev.mysql.com/downloads/mysql/#downloads 推荐下载通用安装方法的TAR包(h ...

  10. animate动画jquery

    <script> $(".change").animate({height:"hide",width:"300px"},&quo ...