HttpMethods(C#.net)
HttpMethods (C#.Net)
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- using System.Net;
- using log4net;
- namespace FaceppSDK
- {
- #region 提交方式
- class HttpMethods : IHttpMethod
- {
- readonly ILog logger = log4net.LogManager.GetLogger(typeof(HttpMethods));
- #region POST
- /// <summary>
- /// HTTP POST方式请求数据
- /// </summary>
- /// <param name="url">URL.</param>
- /// <param name="param">POST的数据</param>
- /// <returns></returns>
- public virtual string HttpPost(string url, string param)
- {
- HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
- request.Method = "POST";
- request.ContentType = "application/x-www-form-urlencoded";
- request.Accept = "*/*";
- request.Timeout = 150000;
- request.AllowAutoRedirect = false;
- StreamWriter requestStream = null;
- WebResponse response = null;
- string responseStr = null;
- try
- {
- requestStream = new StreamWriter(request.GetRequestStream());
- requestStream.Write(param);
- requestStream.Close();
- response = request.GetResponse();
- if (response != null)
- {
- StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
- responseStr = reader.ReadToEnd();
- reader.Close();
- }
- }
- catch (Exception)
- {
- logger.Error("api 访问错误");
- }
- finally
- {
- request = null;
- requestStream = null;
- response = null;
- }
- return responseStr;
- }
- #endregion
- #region Get
- /// <summary>
- /// HTTP GET方式请求数据.
- /// </summary>
- /// <param name="url">URL.</param>
- /// <returns></returns>
- public virtual string HttpGet(string url)
- {
- HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
- request.Method = "GET";
- //request.ContentType = "application/x-www-form-urlencoded";
- request.Accept = "*/*";
- request.Timeout = 150000;
- request.AllowAutoRedirect = false;
- WebResponse response = null;
- string responseStr = null;
- try
- {
- response = request.GetResponse();
- if (response != null)
- {
- StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
- responseStr = reader.ReadToEnd();
- reader.Close();
- }
- }
- catch (Exception)
- {
- logger.Error("api 访问错误");
- }
- finally
- {
- request = null;
- response = null;
- }
- return responseStr;
- }
- #endregion
- #region Post With Pic
- private string HttpPost(string url, IDictionary<object, object> param, string filePath)
- {
- string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
- byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
- HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
- wr.ContentType = "multipart/form-data; boundary=" + boundary;
- wr.Method = "POST";
- wr.KeepAlive = true;
- wr.Credentials = System.Net.CredentialCache.DefaultCredentials;
- Stream rs = wr.GetRequestStream();
- string responseStr = null;
- string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
- foreach (string key in param.Keys)
- {
- rs.Write(boundarybytes, 0, boundarybytes.Length);
- string formitem = string.Format(formdataTemplate, key, param[key]);
- byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
- rs.Write(formitembytes, 0, formitembytes.Length);
- }
- rs.Write(boundarybytes, 0, boundarybytes.Length);
- string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";
- string header = string.Format(headerTemplate, "img", filePath, "text/plain");
- byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
- rs.Write(headerbytes, 0, headerbytes.Length);
- FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
- byte[] buffer = new byte[4096];
- int bytesRead = 0;
- while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
- {
- rs.Write(buffer, 0, bytesRead);
- }
- fileStream.Close();
- byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
- rs.Write(trailer, 0, trailer.Length);
- rs.Close();
- WebResponse wresp = null;
- try
- {
- wresp = wr.GetResponse();
- Stream stream2 = wresp.GetResponseStream();
- StreamReader reader2 = new StreamReader(stream2);
- responseStr = reader2.ReadToEnd();
- logger.Debug(string.Format("File uploaded, server response is: {0}", responseStr));
- }
- catch (Exception ex)
- {
- logger.Error("Error uploading file", ex);
- if (wresp != null)
- {
- wresp.Close();
- wresp = null;
- }
- }
- finally
- {
- wr = null;
- }
- return responseStr;
- }
- #endregion
- #region Post With Pic
- /// <summary>
- /// HTTP POST方式请求数据(带图片)
- /// </summary>
- /// <param name="url">URL</param>
- /// <param name="param">POST的数据</param>
- /// <param name="fileByte">图片</param>
- /// <returns></returns>
- public virtual string HttpPost(string url, IDictionary<object, object> param, byte[] fileByte)
- {
- string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
- byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
- HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
- wr.ContentType = "multipart/form-data; boundary=" + boundary;
- wr.Method = "POST";
- wr.KeepAlive = true;
- wr.Credentials = System.Net.CredentialCache.DefaultCredentials;
- Stream rs = wr.GetRequestStream();
- string responseStr = null;
- string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
- foreach (string key in param.Keys)
- {
- rs.Write(boundarybytes, 0, boundarybytes.Length);
- string formitem = string.Format(formdataTemplate, key, param[key]);
- byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
- rs.Write(formitembytes, 0, formitembytes.Length);
- }
- rs.Write(boundarybytes, 0, boundarybytes.Length);
- string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";
- string header = string.Format(headerTemplate, "img", fileByte, "text/plain");//image/jpeg
- byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
- rs.Write(headerbytes, 0, headerbytes.Length);
- rs.Write(fileByte, 0, fileByte.Length);
- byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
- rs.Write(trailer, 0, trailer.Length);
- rs.Close();
- WebResponse wresp = null;
- try
- {
- wresp = wr.GetResponse();
- Stream stream2 = wresp.GetResponseStream();
- StreamReader reader2 = new StreamReader(stream2);
- responseStr = reader2.ReadToEnd();
- logger.Error(string.Format("File uploaded, server response is: {0}", responseStr));
- }
- catch (Exception ex)
- {
- logger.Error("Error uploading file", ex);
- if (wresp != null)
- {
- wresp.Close();
- wresp = null;
- }
- }
- finally
- {
- wr = null;
- }
- return responseStr;
- }
- #endregion
- }
- #endregion
- #region 枚举
- /// <summary>
- /// 请求方式
- /// </summary>
- public enum Method
- {
- GET,
- POST,
- DELETE
- }
- /// <summary>
- /// 返回格式
- /// </summary>
- public enum Format
- {
- xml,
- json,
- }
- #endregion
- }
HttpMethods(C#.net)的更多相关文章
- nmap --script http-enum,http-headers,http-methods,http-php-version -p 80 目标域
从http服务器上收集到更多地信息 nmap --script http-enum,http-headers,http-methods,http-php-version -p 80 目标域
- ★Kali信息收集★8.Nmap :端口扫描
★Kali信息收集~ 0.Httrack 网站复制机 http://www.cnblogs.com/dunitian/p/5061954.html ★Kali信息收集~ 1.Google Hackin ...
- 解读ASP.NET 5 & MVC6系列(12):基于Lamda表达式的强类型Routing实现
前面的深入理解Routing章节,我们讲到了在MVC中,除了使用默认的ASP.NET 5的路由注册方式,还可以使用基于Attribute的特性(Route和HttpXXX系列方法)来定义.本章,我们将 ...
- nmap脚本扫描使用总结
nmap的脚本默认目录为:/usr/share/nmap/scripts/ Nmap提供的命令行参数如下 -sC: 等价于--script=default,使用默认类别的脚本进行扫描 可更换其他类别 ...
- Nmap扫描手册
By:WHILE扫描类-sTTCP connect()扫描,完整的通话连接,容易被检测,会被记录日志.-sSTCP同步扫描,不完整的通话连接,很少有系统会记入日志.-sUUDP扫描-sAACK扫描用来 ...
- nmap的script参数列表
在新的nmap版本中,添加了script功能的使用.在nmap的安装目录的share/nmap/scripts中,已经有将61个写好的脚本提供. 具体的用法可以参考:http://nmap.org/b ...
- Android--Retrofit+RxJava的简单封装(三)
1,继续接着上一篇的讲讲,话说如果像上一篇这样的话,那么我们每一次请求一个结构都要创建一堆的Retrofit对象,而且代码都是相同的,我们可以试试封装一下 先创建一个HttpMethods类,将Ret ...
- jetty 最后版本类库树, 基本上大多数应用都够了
d:\jetty-distribution-8.1.17.v20150415\lib\annotations\javax.annotation-1.1.0.v201108011116.jarjavax ...
- PUT 还是 POST ?
http://www.oschina.net/translate/put-or-post http://my.oschina.net/u/1263964/blog/268932 这两个方法咋一看都可以 ...
随机推荐
- Python Flask Jinja2模板引擎
模板 简介 模板是一个包含响应文本的文件,其中包含用占位变量表示的动态部分,其具体值只在请 求的上下文中才能知道. 渲染 使用真实值替换变量,再返回最终得到的响应字符串,这一过程 称为渲染.为了渲染模 ...
- Android 数据库框架总结(转)
转自 http://blog.csdn.net/da_caoyuan/article/details/61414626 一:OrmLite 简述: 优点: 1.轻量级:2.使用简单,易上手:3.封装完 ...
- 织梦栏目判断 seotitle的小bug
有的栏目有seotitle(中文字符),有的没有,页面显示需要把seotitle放在括号中,所以进行了以下代码: {dede:field name="seotitle" runph ...
- inception+archery SQL审核平台
关闭防火墙和selinux 宿主机安装mysql,创建archery数据库,并给所有权限,允许远程连接到该数据库 grant all privileges on *.* to 'root'@'%' i ...
- use crunch compression
Crunch is a lossy compression format on top of DXTR texture compression. Textures will be converted ...
- http://www.bugku.com:Bugku——SQL注入1(http://103.238.227.13:10087/)
Bugku——SQL注入1(http://103.238.227.13:10087/) 过滤了几乎所有的关键字,尝试绕过无果之后发现,下面有个xss过滤代码.经搜索得该函数会去掉所有的html标签,所 ...
- javascript简单的滑动效果
利用setInterval实现简单的滑动效果 <!DOCTYPE html> <html lang="en"> <head> <meta ...
- echarts中国地图坐标弹框
echarts链接:http://gallery.echartsjs.com/editor.html?c=xHkdOlpwWz 代码: var geoCoordMap = { '上海': [121.4 ...
- c#、.net、asp.net、asp 、ado.net、.net framework的区别
c#:一种编程语言 .net:一种运行环境 asp.net:基于.netFramework框架下的一种开发技术(相对与asp而言,引入了服务器控件,前后台可分,编译型的编程框架) asp:也是.net ...
- Spring 监听
Spring 中的事件监听的实现 这里我们不讨论事件监听的机制的原理,我们只讨论如何在项目中实现时间监听. spring的事件监听是基于观察者模式.设计开发中.如下类与接口是我们必须要使用的. App ...