WebServiceCallpublic class WebServiceCall
{
public void Call()
{
string url = "http://localhost:1117/WebSite/WebService.asmx";
string data = GetSOAPReuquestBody("");
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.ContentType = "text/xml; charset=utf-8";
req.Method = "POST";
using (Stream reqStream = req.GetRequestStream())
{
byte[] reqData = Encoding.UTF8.GetBytes(data);
reqStream.Write(reqData, , reqData.Length);
} HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
Console.WriteLine(resp.StatusCode);
foreach (var item in resp.Headers.AllKeys)
{
Console.WriteLine(item + " : " + resp.Headers[item]);
}
using (StreamReader reader = new StreamReader(resp.GetResponseStream(), Encoding.UTF8))
{
Console.WriteLine(reader.ReadToEnd());
} }
public void Call2()
{
string url = "http://localhost:1117/WebSite/WebService.asmx/GetNumber";
string data = "id=3";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";
using (Stream reqStream = req.GetRequestStream())
{
byte[] reqData = Encoding.UTF8.GetBytes(data);
reqStream.Write(reqData, , reqData.Length);
} HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
Console.WriteLine(resp.StatusCode);
foreach (var item in resp.Headers.AllKeys)
{
Console.WriteLine(item + " : " + resp.Headers[item]);
}
using (StreamReader reader = new StreamReader(resp.GetResponseStream(), Encoding.UTF8))
{
Console.WriteLine(reader.ReadToEnd());
} } public string GetSOAPReuquestBody(string param)
{
StringBuilder soap = new StringBuilder();
soap.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
soap.Append("<soap12:Envelope xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">");
soap.Append("<soap12:Body>");
soap.Append("<GetNumber xmlns=\"http://tempuri.org/\">");
soap.Append("<id>");
soap.Append(param);
soap.Append("</id>");
soap.Append("</GetNumber>");
soap.Append("</soap12:Body>");
soap.Append("</soap12:Envelope>");
return soap.ToString();
}
}

http://www.cnblogs.com/disappearwind/articles/2633760.html

C#调用WebService的简单方式的更多相关文章

  1. 【C#】 创建、部署和调用WebService的简单示例

    C# 创建.部署和调用WebService的简单示例 <div class="postBody"> <div id="cnblogs_post_body ...

  2. C# 创建、部署和调用WebService的简单示例 (转)

    C# 创建.部署和调用WebService的简单示例(转)  转自 https://www.cnblogs.com/Brambling/p/7266482.html  webservice 可以用于分 ...

  3. C# 创建、部署和调用WebService的简单示例

    废话不多说,下面开始创建一个简单的webservice的例子.这里我用的是Visual Studio 2015开发工具. 首先创建一个空的Web应用程序. 然后鼠标右键点击项目,选择 添加>新建 ...

  4. axis2调用webService几种方式

    主要有三种方式: 第一RPC方式,不生成客户端代码 第二,document方式,不生成客户端代码 第三,用wsdl2java工具,生成客户端方式调用 java代码: package samples.q ...

  5. 调用WebService的简单方法

    package com.xxx.webservice.internal.test; import java.net.MalformedURLException; import java.net.URL ...

  6. axis调用webservice的简单方法

    package com.service; import org.apache.axis.client.Call; import org.apache.axis.client.Service; publ ...

  7. 动态调用WebService接口的几种方式

    一.什么是WebService? 这里就不再赘述了,想要了解的====>传送门 二.为什么要动态调用WebService接口? 一般在C#开发中调用webService服务中的接口都是通过引用过 ...

  8. asp.net访问WebService的各种方式

    WebService的访问形式主要有:SOAP调用.XMLHTTP POST.GET调用.MicroSoft.XMLDOMC调用.webbehavior.htc调用 我们知道的在C#后台本地调用Web ...

  9. Httpclient远程调用WebService示例(Eclipse+httpclient)

    package cn.com.taiji.pos.httpserver; import java.io.BufferedInputStream;import java.io.ByteArrayOutp ...

随机推荐

  1. LeetCode(103): 二叉树的锯齿形层次遍历

    Medium! 题目描述: 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行). 例如:给定二叉树 [3,9,20,null,nul ...

  2. 用mybatis做数据库处理 代码中的字段大小写 要和mapper映射设置的大小写一致(这TM不废话么,原谅我渣!~~)

    简单描述情况:其实这个问题怎么说呢,有人给你说,你肯定能意识到,必须大小写对应的嘛.emmmm我现在才意识到是因为:自己在下边敲代码,配的mapper文件并没有把属性变量,和数据库里的段单独提出来做映 ...

  3. git 注意事项

    1,用户凭证 github的两种url地址 http      ssh :由于Git和Github交互操作可能会很频繁,那么一定少了用户授权的操作,为了防止每次操作重复输入用户名和密码,Git提供了两 ...

  4. node.js 框架express关于报错页面的配置

    1.声明报错的方法,以及相对应的页面 //把数据库的调用方法配置到请求中 server.use((req, res, next) => { //把数据库存入req中 req.db = db; / ...

  5. linux安装python3+selenium

    安装笔记 当前安装使用centos7 安装python3 1.下载 [admin@ ~] wget https://www.python.org/ftp/python/3.6.0/Python-3.6 ...

  6. Android Studio 删除多余的虚拟设备(Virtual Device)

    操作系统:Windows 10 x64 IDE:Android Studio 3.2.1 菜单:Tools > AVD Manager 在Android Virtual Device Manag ...

  7. 论文阅读笔记三十九:Accurate Single Stage Detector Using Recurrent Rolling Convolution(RRC CVPR2017)

    论文源址:https://arxiv.org/abs/1704.05776 开源代码:https://github.com/xiaohaoChen/rrc_detection 摘要 大多数目标检测及定 ...

  8. 集腋成裘-03-css基础-02

    1.1 三种写法 内嵌式:样式只作用于当前文件,没有真正实现结构表现分离 外链式:作用范围是当前站点,真正实现了内容与表现分离 行内样式:仅限于当前标签,结构混在一起 1.2 标签分类 1.2.1 块 ...

  9. 查看CPU 内存 硬盘 网络 查看进程使用的文件 uptime top ps -aux vmstat iostat iotop nload iptraf nethogs

    #安装命令 yum install  sysstat #包含 iostat vmstat yum install iotop yum install  nload yum install iptraf ...

  10. SQLServer索引及统计信息

    索引除了提高性能,还能维护数据库. 索引是一种存储结构,主要以B-Tree形式存储信息. B-Tree的定义: 1.每个节点最多只有m个节点(m>=2) 2.除了根节点和叶子节点外的每个节点上最 ...