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. cf round546 cde

    第一题会卡一下同时用set和cin.. 其他的注意下矩阵对角线下标的应用即可 #include<bits/stdc++.h> using namespace std; #define ma ...

  2. Python字典(Dictionary)

    Python中字典与类表类似,也是可变序列,不过与列表不同,他是无序的可变序列,保存的内容是以键 - 值对的形式存放的.类似我们的新华字典,他可以把拼音和汉字关联起来,通过音节表可以快速的找到想要的字 ...

  3. js获取url协议、url, 端口号等信息路由信息

    以路径为 http://www.baidu.com  为例 console.log("location:"+window.location.href); >> &quo ...

  4. 阿里云服务器配置SSL证书成功开启Https(记录趟过的各种坑)

    环境: 阿里云云服务器    Windows Server 2008 标准版 SP2 中文版(趁1212优惠买的一年的水货配置) 阿里云购买的域名(已备案.已解析) 服务器:phpstudy:php5 ...

  5. 优先选择nullptr而不是0和NULL

    我们知道:0是一个int,而不是一个指针.如果C++在一个只有指针才能够使用的上下文中发现它只有一个0,那么它会勉强将0解释成空指针,但那时一种倒退行为.C++的主要方针是0就是一个int,而不是指针 ...

  6. gerrit原理

      个人理解: 这个就是审核代码是否合理性的工具,一般是资深研发人工确认代码是否存在缺陷,通过发送邮件通知变化. 也可理解为这个是个git服务器,多一个代码审查的功能. 但是它是个web界面,方便管理 ...

  7. 基于nginx的WebSocket反向代理

    系统:windows 版本:nginx 1.4.5 配置内容: location ~ \.do$ {             proxy_pass   http://192.168.20.102:80 ...

  8. zabbix 3.2.4 使用详解

    一:zabbix简介及原理 二:zabbix添加主机: /usr/share/zabbix/include/locales.inc.php   #这里为zabbix语言包路径‘zh_CN’ 为true ...

  9. move_uploaded_file中文乱码

    move_uploaded_file($_FILES['file']['tmp_name'],iconv("UTF-8","gb2312",$filname)

  10. python之requests 乱七八糟

    1.预配置 import requests ss = requests.Session() ss.headers.update({'user-agent':'Mozilla/5.0 (Windows ...