首页 技术 正文
技术 2022年11月7日
0 收藏 641 点赞 731 浏览 2253 个字

转自 https://www.cnblogs.com/Brambling/p/7266482.html

之前调用 WebService 都是直接添加服务引用,然后调用 WebService 方法的,最近发现还可以使用 Http 请求调用 WebService。这里还想说一句,还是 web api 的调用简单。

WebService 服务端代码:

C# 使用 HttpPost 请求调用 WebService (转)

   public class WebServiceDemo : System.Web.Services.WebService
{ [WebMethod]
public string HelloWorld()
{
return "Hello World";
} [WebMethod]
public string Sum(string param1, string param2)
{
int num1 = Convert.ToInt32(param1);
int num2 = Convert.ToInt32(param2); int sum = num1 + num2; return sum.ToString();
}
}

C# 使用 HttpPost 请求调用 WebService (转)

很简单的代码,只是用于演示。

客户端调用代码:

C# 使用 HttpPost 请求调用 WebService (转)

   class Program
{
static void Main(string[] args)
{
Program program = new Program();
string url = "http://localhost:12544/WebServiceDemo.asmx";
string method = "Sum";
string num1 = "1";
string num2 = "2"; string result = program.HttpPostWebService(url, method, num1, num2); Console.WriteLine(result);
Console.ReadKey();
} public string HttpPostWebService(string url,string method,string num1,string num2)
{
string result = string.Empty;
string param = string.Empty;
byte[] bytes = null; Stream writer = null;
HttpWebRequest request = null;
HttpWebResponse response = null; param = HttpUtility.UrlEncode("param1") + "=" + HttpUtility.UrlEncode(num1) + "&" + HttpUtility.UrlEncode("param2") + "=" + HttpUtility.UrlEncode(num2);
bytes = Encoding.UTF8.GetBytes(param); request = (HttpWebRequest)WebRequest.Create(url + "/" + method);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = bytes.Length; try
{
writer = request.GetRequestStream(); //获取用于写入请求数据的Stream对象
}
catch (Exception ex)
{
return "";
} writer.Write(bytes, 0, bytes.Length); //把参数数据写入请求数据流
writer.Close(); try
{
response = (HttpWebResponse)request.GetResponse(); //获得响应
}
catch (WebException ex)
{
return "";
} #region 这种方式读取到的是一个返回的结果字符串
Stream stream = response.GetResponseStream(); //获取响应流
XmlTextReader Reader = new XmlTextReader(stream);
Reader.MoveToContent();
result = Reader.ReadInnerXml();
#endregion #region 这种方式读取到的是一个Xml格式的字符串
//StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
//result = reader.ReadToEnd();
#endregion response.Dispose();
response.Close(); //reader.Close();
//reader.Dispose(); Reader.Dispose();
Reader.Close(); stream.Dispose();
stream.Close(); return result;
}
}

C# 使用 HttpPost 请求调用 WebService (转)

第一种读取方式的返回结果:

C# 使用 HttpPost 请求调用 WebService (转)

第二种读取方式的返回结果:

C# 使用 HttpPost 请求调用 WebService (转)

PS:如果遇到调用时报错,可以尝试在服务端(即WebService)的 web.config 配置中添加如下配置节点。

C# 使用 HttpPost 请求调用 WebService (转)

 <system.web>
<webServices>
<protocols>
<add name="HttpPost" />
</protocols>
</webServices>
</system.web>

C# 使用 HttpPost 请求调用 WebService (转)

参考:

http://www.cnblogs.com/caiwenz/p/3910566.html

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,487
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,487
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,127
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289