WebClient调用远程接口
来源:互联网 发布:linux java 打包war 编辑:程序博客网 时间:2024/05/22 01:50
/// <summary>
/// 调用接口,发送的参数
/// </summary>
class SendParams
{
internal int DeliveryID { get; set; }
internal int OrderStatusID { get; set; }
internal String Reason { get; set; }
}
/// <summary>
/// 接口返回的结果
/// </summary>
class ReceiveResult
{
internal int code { get; set; }
internal String msg { get; set; }
internal String result { get; set; }
}
/// <summary>
/// 调用远程接口
/// </summary>
/// <param name="url">接口地址</param>
/// <param name="sendParams">接口参数</param>
/// <returns>返回结果对象</returns>
private ReceiveResult CallRemoteInterface(String url,SendParams sendParams)
{
string postData = "DeliveryID=" + sendParams.DeliveryID + "&OrderStatusID=" + sendParams.OrderStatusID + "&Reason=" + sendParams.Reason;
byte[] bytes = Encoding.UTF8.GetBytes(postData);
WebClient client = new WebClient();
client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
client.Headers.Add("ContentLength", postData.Length.ToString());
String jsonResult = client.UploadData(url, "POST", bytes).ToString();
//把返回的json构造成c#对象
ReceiveResult receiveResult = null;
DataContractJsonSerializer serializer = new DataContractJsonSerializer(receiveResult.GetType());
using (MemoryStream mStream = new MemoryStream(Encoding.UTF8.GetBytes(jsonResult)))
{
receiveResult = (ReceiveResult)serializer.ReadObject(mStream);
}
return receiveResult;
}
/// 调用接口,发送的参数
/// </summary>
class SendParams
{
internal int DeliveryID { get; set; }
internal int OrderStatusID { get; set; }
internal String Reason { get; set; }
}
/// <summary>
/// 接口返回的结果
/// </summary>
class ReceiveResult
{
internal int code { get; set; }
internal String msg { get; set; }
internal String result { get; set; }
}
/// <summary>
/// 调用远程接口
/// </summary>
/// <param name="url">接口地址</param>
/// <param name="sendParams">接口参数</param>
/// <returns>返回结果对象</returns>
private ReceiveResult CallRemoteInterface(String url,SendParams sendParams)
{
string postData = "DeliveryID=" + sendParams.DeliveryID + "&OrderStatusID=" + sendParams.OrderStatusID + "&Reason=" + sendParams.Reason;
byte[] bytes = Encoding.UTF8.GetBytes(postData);
WebClient client = new WebClient();
client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
client.Headers.Add("ContentLength", postData.Length.ToString());
String jsonResult = client.UploadData(url, "POST", bytes).ToString();
//把返回的json构造成c#对象
ReceiveResult receiveResult = null;
DataContractJsonSerializer serializer = new DataContractJsonSerializer(receiveResult.GetType());
using (MemoryStream mStream = new MemoryStream(Encoding.UTF8.GetBytes(jsonResult)))
{
receiveResult = (ReceiveResult)serializer.ReadObject(mStream);
}
return receiveResult;
}
0 0
- WebClient调用远程接口
- http协议接口调用之WebClient
- RMI(远程接口调用)
- RMI(远程接口调用)
- RMI(远程接口调用)
- RMI(远程接口调用)
- RestTemplate远程接口调用
- RMI远程接口调用
- RMI(远程接口调用)
- CXF 入门: 远程接口调用
- Python调用远程Socket接口
- 远程接口调用HttpURLConnection,URL
- hessian 实现远程接口调用
- axis2 远程调用webservice接口
- Axis远程调用WebService接口
- 远程调用 api接口 post
- scala远程调用thrift接口
- 调用远程接口,返回数据
- 最快速的Android开发环境搭建ADT-Bundle及完成第一个程序Hello World~
- 黑马程序员-java基础第5、6天-面向对象(类的组成部分分析)
- 电脑蓝屏代码大全
- 邦盛 面试
- Poi excel
- WebClient调用远程接口
- HttpClient 教程 (五)
- 使用脚本合并多个Microsoft Visio绘图文件
- 配置request、session
- IT行业职位发布量仍稳居榜首
- 《汇编语言》(王爽著)的读书笔记(1)
- Lua的类实现继承、多态以及setmetatable方法
- 2012C++第8周项目1 - 实现复数类中的运算符重载 【项目1-实现复数类中的运算符重载】
- java Map