C# 客户端 调用Java版的REST风格的web Service
来源:互联网 发布:网络测试 编辑:程序博客网 时间:2024/05/17 23:11
前言:
关于Web Service的相关知识可以查看本人博客:http://blog.csdn.net/luchengtao11/article/details/72054738
本文所述代码托管在Github:https://github.com/Wuchenwcf/MyCode/tree/master/JAVA/HelloWorldWebService
一、运行服务端
JAVA 版的webService网上有很多教程,这里直接提供一份编写好的Helloworld,可从上述连接中下载。
连接中提供一份教程文档,按照教程把服务器端跑起来
结果如下:
当然,我们不是为了通过浏览器访问它的,那就没意思了。
二、编写C#版本的客户端
直接建立控制台工程就可以了,记得要使用.net 4.0的框架,然后添加System.Web的连接
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.IO;using System.Net;using System.Web;namespace WebServiceTest{ class Program { static void Main(string[] args) { //以Get方式调用 HttpWebRequest request = WebRequest.Create("http://localhost:8080/wildfly-helloworld-rs/rest/get/007") as HttpWebRequest; using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { StreamReader reader = new StreamReader(response.GetResponseStream()); Console.WriteLine(reader.ReadToEnd()); } //以Post方式调用 Uri address = new Uri("http://localhost:8080/wildfly-helloworld-rs/rest/create"); HttpWebRequest request1 = WebRequest.Create(address) as HttpWebRequest; request1.Method = "POST"; request1.ContentType = "application/x-www-form-urlencoded"; String id = "789"; String name = "test"; StringBuilder data = new StringBuilder(); //调用HttpUtility需要在.net 4.0框架下,并且添加System.web.dll引用,请自行谷歌 data.Append("id=" + System.Web.HttpUtility.UrlEncode(id)); data.Append("&name=" + System.Web.HttpUtility.UrlEncode(name)); byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString()); request1.ContentLength = byteData.Length; using (Stream postStream = request1.GetRequestStream()) { postStream.Write(byteData, 0, byteData.Length); } using (HttpWebResponse response1 = request1.GetResponse() as HttpWebResponse) { StreamReader reader = new StreamReader(response1.GetResponseStream()); Console.WriteLine(reader.ReadToEnd()); } } }}
客户端的运行结果如下:可以看到,get到了服务端的信息。
有了以上Demo,就可以进而编写花样百出的应用了。
阅读全文
0 0
- C# 客户端 调用Java版的REST风格的web Service
- 通过 Jersey 客户端 API 调用 REST 风格的 Web 服务
- 通过 Jersey 客户端 API 调用 REST 风格的 Web 服务
- 通过Jersey客户端API调用REST风格的Web服务
- 通过 Jersey 客户端 API 调用 REST 风格的 Web 服务
- 通过 Jersey 客户端 API 调用 REST 风格的 Web 服务
- 通过 Jersey 客户端 API 调用 REST 风格的 Web 服务
- Jersey客户端API调用REST风格的Web服务
- 通过Jersey客户端API调用REST风格的Web服务
- 构建REST风格的Web Service
- 构建REST风格的Web Service
- 构建REST风格的Web Service
- 构建REST风格的Web Service
- 构建REST风格的Web Service
- 构建REST风格的Web Service
- Rest风格WEB服务(Rest Style Web Service)的真相
- Rest风格WEB服务(Rest Style Web Service)的真相
- Rest风格WEB服务(Rest Style Web Service)的真相
- Vue.js前后端同构方案之准备篇——代码优化
- Linux最小安装
- http请求方法get和post的区别
- 永恒之蓝 网管必备知识库 分分钟排除险情
- ECNU Problem #3263 丽娃河的狼人传说(区间贪心)
- C# 客户端 调用Java版的REST风格的web Service
- Wireshark命令行工具tshark详解(含例子)
- 二分查找算法
- python--PIL操作像素例子
- Tarjan算法 模板
- 错误的索引统计信息导致oracle expdp导500多G的大分区表时hang住
- 刻度尺选择控件
- 位运算与逻辑运算
- opencv在ubuntu下的安装