请求rest web服务client
RestClient
using System;
using System.Globalization;
using System.IO;
using System.Net;
using System.Text; [assembly: CLSCompliant(true)]
namespace Downloader
{
public enum HttpVerb
{
Get,
Post,
Put,
Delete
} /// <summary>
/// Restful Service Client
/// </summary>
public class RestClient
{
/// <summary>
/// application/json
/// </summary>
private string _contentType = "application/json";
/// <summary>
/// 15000
/// </summary>
private int _timeout = ;
/// <summary>
/// Service Uri
/// </summary>
public string EndPoint { get; set; }
/// <summary>
/// Post || Get
/// </summary>
public HttpVerb Method { get; set; }
/// <summary>
/// Default "text/xml"
/// </summary>
public string ContentType { get { return _contentType; } set { _contentType = value; } }
/// <summary>
/// eg. "'test':'abcd'"
/// </summary>
public string PostData { get; set; }
/// <summary>
/// TimeOut ms
/// </summary>
public int Timeout { get { return _timeout; } set { _timeout = value; } }
/// <summary>
/// Default Constructor
/// </summary>
public RestClient()
{
EndPoint = "";
Method = HttpVerb.Get;
ContentType = _contentType;
PostData = "{}";
Timeout = _timeout;
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="endpoint">Service Uri</param>
public RestClient(string endpoint)
{
EndPoint = endpoint;
Method = HttpVerb.Get;
ContentType = _contentType;
PostData = "{}";
Timeout = _timeout;
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="endpoint">Service Uri</param>
/// <param name="method">Post || Get</param>
public RestClient(string endpoint, HttpVerb method)
{
EndPoint = endpoint;
Method = method;
ContentType = _contentType;
PostData = "{}";
Timeout = _timeout;
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="endpoint">Service Uri</param>
/// <param name="method">Post || Get</param>
/// <param name="postData">eg. "{'test':'abcd'}"</param>
public RestClient(string endpoint, HttpVerb method, string postData)
{
EndPoint = endpoint;
Method = method;
ContentType = _contentType;
PostData = postData;
Timeout = _timeout;
}
/// <summary>
/// Request server
/// </summary>
/// <returns></returns>
public string MakeRequest()
{
return MakeRequest("");
}
/// <summary>
/// Request server
/// </summary>
/// <param name="parameters">eg. "?q=1234"</param>
/// <returns></returns>
public string MakeRequest(string parameters)
{
var service = new Uri(EndPoint + parameters);
var request = WebRequest.Create(service);
request.Timeout = Timeout;
request.Method = Method.ToString().ToUpper(CultureInfo.CurrentCulture);
request.ContentLength = ;
request.ContentType = ContentType; if (Method == HttpVerb.Post)
{
// PostData = "{" + PostData + "}";
var bytes =Encoding.UTF8.GetBytes(PostData);
request.ContentLength = bytes.Length; using (var writeStream = request.GetRequestStream())
{
writeStream.Write(bytes, , bytes.Length);
}
} using (var response = (HttpWebResponse)request.GetResponse())
{
var responseValue = string.Empty;
if (response.StatusCode != HttpStatusCode.OK)
{
var message = String.Format(CultureInfo.CurrentCulture, "Request failed. Received HTTP {0}", response.StatusCode);
throw new WebException(message);
}
using (var responseStream = response.GetResponseStream())
{
if (responseStream != null)
using (var reader = new StreamReader(responseStream))
{
responseValue = reader.ReadToEnd();
}
}
return responseValue;
}
}
/// <summary>
/// 同步更新二进制文件
/// </summary>
/// <param name="parameters">URL参数</param>
/// <param name="postData">表单提交参数</param>
/// <returns></returns>
public byte[] MakeRequest(string parameters, string postData)
{
var service = new Uri(EndPoint + parameters);
var request = WebRequest.Create(service);
request.Timeout = Timeout;
request.Method = Method.ToString().ToUpper(CultureInfo.CurrentCulture);
request.ContentLength = ;
request.ContentType = ContentType; if (Method == HttpVerb.Post)
{
// PostData = "{" + postData + "}";
var bytes = Encoding.UTF8.GetBytes(PostData);
request.ContentLength = bytes.Length; using (var writeStream = request.GetRequestStream())
{
writeStream.Write(bytes, , bytes.Length);
}
} using (var response = (HttpWebResponse)request.GetResponse())
{
if (response.StatusCode != HttpStatusCode.OK)
{
var message = String.Format(CultureInfo.CurrentCulture, "Request failed. Received HTTP {0}", response.StatusCode);
throw new WebException(message);
}
using (var result = new MemoryStream())
{
const int bufferLen = ;
var buffer = new byte[bufferLen];
using (var responseStream = response.GetResponseStream())
{
var len = responseStream.Read(buffer, , bufferLen);
while (len > )
{
result.Write(buffer, , len);
len = responseStream.Read(buffer, , bufferLen);
}
}
return result.ToArray();
}
}
} } }
请求rest web服务client的更多相关文章
- 利用python httplib模块 发送Post请求测试web服务是否正常起来!
最近在学习python,恰好老大最近让我搞个基于post请求测试web服务是否正常启用的小监控,上网查了下资料,发现强大的Python恰好能够用上,所以自己现学现卖,顺便锻炼下自己. 由于本人也刚接触 ...
- 如何检测 Web 服务请求丢失问题
导读 『StabilityGuide』是阿里多位阿里技术工程师共同发起的稳定性领域的知识库开源项目,涵盖性能压测.故障演练.JVM.应用容器.服务框架.流量调度.监控.诊断等多个技术领域,以更结构化的 ...
- Azure机器学习入门(四)模型发布为Web服务
接Azure机器学习(三)创建Azure机器学习实验,下一步便是真正地将Azure机器学习的预测模型发布为Web服务.要启用Web服务发布任务,首先点击底端导航栏的运行即"Run" ...
- Web服务端性能提升实践
随着互联网的不断发展,日常生活中越来越多的需求通过网络来实现,从衣食住行到金融教育,从口袋到身份,人们无时无刻不依赖着网络,而且越来越多的人通过网络来完成自己的需求. 作为直接面对来自客户请求的Web ...
- WEB服务动静结合
基本介绍 1)WEB服务仅能处理静态请求,如果处理动态请求则需要对应的动态资源服务软件,即:应用程序服务软件 2)常见的应用服务软件有:PHP.Java.Python等 3)问题:WEB服务如何与外部 ...
- 通过 Jersey Http请求头,Http响应头,客户端 API 调用 REST 风格的 Web 服务
原地址:http://blog.csdn.net/li575098618/article/details/47853263 Jersey 1.0 是一个开源的.可以用于生产环境的 JAX-RS(RES ...
- 记录一个调试REST风格的web服务的client
coogle浏览器的advanced rest client很好用,记录一下,脑子不好,容易忘,,可以在chrome 的网上应用店添加 Rest client是用来调试REST风格的Web服务,接收P ...
- [WEB API] CLIENT 指定请求及回应格式(XML/JSON)
[Web API] Client 指定请求及响应格式(xml/json) Web API 支持的格式请参考 http://www.asp.net/web-api/overview/formats-an ...
- HTTP协议基础与web服务的重定向,跳转以及请求转发
JavaWeb中,HttpServletRequest与HttpServletResponse几乎是处理各种请求与操作必备的参数,与原始的ServletRequest/ServletResponse相 ...
随机推荐
- jquery validate 在ajax提交表单下的验证方法
$(function() { var method='${method }'; if(method == 'edit'){ url="${ctx}/commodity/typeReN ...
- iOS开发之滤镜的使用技巧(CoreImage)
一.滤镜的内容和效果是比较多并且复杂的 ,学习滤镜需要技巧 如下: 两个输出语句解决滤镜的属性选择问题: 1.查询效果分类中包含什么效果按住command 点击CIFilter 进入接口文件 找到第1 ...
- ASP.NET 配置KindEditor文本编辑器
ASP.NET 配置KindEditor文本编辑器 跟着这篇博客做了两个小时,只搞出了下面这么个东西. 时间浪费在了原博客里这样的一句话:将kindeditor/asp.net/bin/LitJSON ...
- DAS、SAN、NAS的区别
在服务企业的过程中,经常会接触到存储,但是又容易和市面上的一些什么NAS等混淆. DAS 与主机直连的存储设备 NAS 存储设备自带文件系统 SAN 存储设备的互联依赖网络,比如IP-SAN, FC- ...
- 【转】selenium之 定位以及切换frame
转载自:http://www.voidcn.com/blog/huilan_same/article/p-6155896.html 很多人在用selenium定位页面元素的时候会遇到定位不到的问题,明 ...
- ssh和putty
SH(Secure Shell的缩写),由 IETF 的网络工作小组(Network Working Group)所制定:SSH 为建立在应用层和传输层基础上的安全协议.SSH 是目前较可靠,专为远程 ...
- bugfree安装
1.下载xampp文件:xampp-linux-x64-5.5.30-3-installer.run 2.安装此文件,用root账号安装,安装命令:./xampp-linux-x64-5.5.30-3 ...
- SAP物料批次管理配置及操作手册(轉載)
这个有待学习 业务说明需要先熟悉基本的物料管理(MM).生产计划管理(PP).销售与分销管理(SD)的相关知识.在化工.制药.快消.汽车零部件等行业,为了进行质量的跟踪,往往需要使用批次管理来监控质量 ...
- Install PaddlePaddle (Parallel Distributed Deep Learning)
Step 1: Install docker on your linux system (My linux is fedora) https://docs.docker.com/engine/inst ...
- [转]Java 8:不要再用循环了
以下内容为转载,没有在jdk8中测试,具体业务场景是否存在BUG或使用需要注意的地方有待测试. ------------------分割线---------------------- 正如我之前所写的 ...