C#调用WebService的简单方式
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的简单方式的更多相关文章
- 【C#】 创建、部署和调用WebService的简单示例
C# 创建.部署和调用WebService的简单示例 <div class="postBody"> <div id="cnblogs_post_body ...
- C# 创建、部署和调用WebService的简单示例 (转)
C# 创建.部署和调用WebService的简单示例(转) 转自 https://www.cnblogs.com/Brambling/p/7266482.html webservice 可以用于分 ...
- C# 创建、部署和调用WebService的简单示例
废话不多说,下面开始创建一个简单的webservice的例子.这里我用的是Visual Studio 2015开发工具. 首先创建一个空的Web应用程序. 然后鼠标右键点击项目,选择 添加>新建 ...
- axis2调用webService几种方式
主要有三种方式: 第一RPC方式,不生成客户端代码 第二,document方式,不生成客户端代码 第三,用wsdl2java工具,生成客户端方式调用 java代码: package samples.q ...
- 调用WebService的简单方法
package com.xxx.webservice.internal.test; import java.net.MalformedURLException; import java.net.URL ...
- axis调用webservice的简单方法
package com.service; import org.apache.axis.client.Call; import org.apache.axis.client.Service; publ ...
- 动态调用WebService接口的几种方式
一.什么是WebService? 这里就不再赘述了,想要了解的====>传送门 二.为什么要动态调用WebService接口? 一般在C#开发中调用webService服务中的接口都是通过引用过 ...
- asp.net访问WebService的各种方式
WebService的访问形式主要有:SOAP调用.XMLHTTP POST.GET调用.MicroSoft.XMLDOMC调用.webbehavior.htc调用 我们知道的在C#后台本地调用Web ...
- Httpclient远程调用WebService示例(Eclipse+httpclient)
package cn.com.taiji.pos.httpserver; import java.io.BufferedInputStream;import java.io.ByteArrayOutp ...
随机推荐
- python并发编程之进程池,线程池,协程
需要注意一下不能无限的开进程,不能无限的开线程最常用的就是开进程池,开线程池.其中回调函数非常重要回调函数其实可以作为一种编程思想,谁好了谁就去掉 只要你用并发,就会有锁的问题,但是你不能一直去自己加 ...
- mysql通配符使用
mysql通配符使用: w3cchool 在mysql查询中,经常会用到通配符,而且mysql的通配符和pgsql是有所不同的,甚至mysql中还可以使用正则表达式.本文就为大家带来mysql查询中通 ...
- RabbitMQ中客户端的Channel类里各方法释义
// The contents of this file are subject to the Mozilla Public License // Version 1.1 (the "Lic ...
- spring cloud config--client
概述 之前我们简单的搭建了一个单点的config-server服务,实现配置文件的统一管理,本次文章将实现config-client是如何从config-server中获取到统一配置文件信息的 1.创 ...
- Tensorflow生成唐诗和歌词(下)
整个工程使用的是Windows版pyCharm和tensorflow. 源码地址:https://github.com/Irvinglove/tensorflow_poems/tree/master ...
- python 装饰器(1)
# 装饰器,对一个函数打扮 # def foo():# print("foo")# foo# foo()# def test1():# print('-----1-----')# ...
- 史上最简单的SpringCloud教程 | 第五篇: 路由网关(zuul)
在微服务架构中,需要几个基础的服务治理组件,包括服务注册与发现.服务消费.负载均衡.断路器.智能路由.配置管理等,由这几个基础组件相互协作,共同组建了一个简单的微服务系统.一个简答的微服务系统如下图: ...
- asp.net core MVC 控制器,接收参数,数据绑定
1.参数 HttpRequest HttpRequest 是用户请求对象 QueryString Form Cookie Session Header 实例: public IActionResult ...
- 升级到 Android Studio 3.0 + Gradle 4.1 遇到的一些坑及解决方案
问题一: Cannot set the value of read-only property 'outputFile' for ApkVariantOutputImpl_Decorated{apkD ...
- python--自己实现的单链表常用功能
最近一个月,就耗在这上面吧. 很有收获的. # coding = utf-8 # 单向链表 class Node: def __init__(self, new_data): self.data = ...