using System;
using System.Web.Services;
using System.Net;
using System.IO;
namespace WebServiceClient
{
class Program
{
static void Main(string[] args)
{
// 创建一个WebClient实例,用于发送HTTP请求
using (WebClient client = new WebClient())
{
// 设置请求的URL,这里假设WebService的地址是http://example.com/Service.asmx?op=HelloWorld
string url = "http://example.com/Service.asmx/HelloWorld";
// 发送GET请求并获取响应
try
{
string response = client.DownloadString(url);
Console.WriteLine("Response from WebService: " + response);
}
catch (WebException ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}
}
}
}
WebClient 是 .NET 提供的一个简单类,用于发送 HTTP 请求和接收响应。它简化了网络编程,适合简单的 HTTP 操作。url 变量中存储了 WebService 的调用地址。这里的例子假设 WebService 的地址是 http://example.com/Service.asmx?op=HelloWorld,你需要根据实际情况替换为你要调用的 WebService 地址。client.DownloadString(url) 方法发送 GET 请求,并将返回的字符串结果存储在 response 变量中。try-catch 块捕获可能发生的异常(如网络错误),并在控制台输出错误信息。这个示例展示了如何通过 C# 调用一个简单的 WebService 接口。如果你需要调用更复杂的接口(如带有参数的接口或 POST 请求),可以进一步扩展代码。
上一篇:c#websocket
下一篇:c#窗体控件随窗体变大
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站