在.net 6.0中 调用远程服务器web服务,Webservices(xxx.asmx) ,RESTful 风格,2种解决方案。

1.使用 Connected Services**:**

  • 右键单击您的项目,选择 "Add"(添加)-> "Connected Services"(已连接的服务)。

  • 在 "Connected Services" 对话框中,选择 "WCF Web Service"。

  • 在 "WCF Web Service" 对话框中,输入 xxx.asmx 的 URL 地址(例如:http://A.B.C.com/ABCWebService/product/ProductService.asmx),然后点击"转到",然后在点击下方服务中的xxxSoap,右边操作框里面会显示所有方法,然后在输入一个命名空间(例如:CBEC.Web.ProductWebService),然后点击 "下一步"(前往)。截图如下:

  • Visual Studio 将会获取 xxx.asmx 中的方法列表。选择您想要添加的方法,然后点击 "Finish"(完成)。

  • Visual Studio 将为您生成一个代理类,该代理类包含远程服务器上的方法,您可以像调用本地方法一样使用这些方法。

示例代码:

cs 复制代码
using System;
using System.Threading.Tasks;
using YourServiceReference; // 请将 YourServiceReference 替换为实际的服务引用名称

class Program
{
    static async Task Main()
    {
        // 创建服务引用的客户端
        var client = new HuiLvServiceSoapClient(HuiLvServiceSoapClient.EndpointConfiguration.HuiLvServiceSoap);

        // 调用远程服务器的方法
        var result = await client.YourRemoteMethodAsync(param1, param2);
        
        // 处理返回的结果
        Console.WriteLine(result);
    }
}

2.使用 HTTP 客户端:

如果 HuiLvService.asmx 提供的是 RESTful 风格的 Web API,您可以使用 HttpClient 类来进行 HTTP 请求。这种方法适用于 Web API 返回的数据是 JSON 或其他常见格式的情况。

示例代码

cs 复制代码
using System;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;

class Program
{
    static async Task Main()
    {
        using var client = new HttpClient();

        // 设置基础地址
        client.BaseAddress = new Uri("https://your-remote-server-url/");

        // 构造请求数据
        var requestData = new YourRequestData { /*...*/ };

        // 将请求数据序列化为 JSON
        var requestDataJson = JsonSerializer.Serialize(requestData);

        // 构造 HTTP 请求
        var content = new StringContent(requestDataJson, System.Text.Encoding.UTF8, "application/json");

        // 发起 POST 请求
        var response = await client.PostAsync("HuiLvService.asmx/YourRemoteMethod", content);

        // 处理响应
        if (response.IsSuccessStatusCode)
        {
            var responseContent = await response.Content.ReadAsStringAsync();
            // 将响应内容反序列化为结果类型
            var result = JsonSerializer.Deserialize<YourResultType>(responseContent);
            // 处理返回的结果
            Console.WriteLine(result);
        }
        else
        {
            Console.WriteLine("HTTP 请求失败:" + response.StatusCode);
        }
    }
}

请注意,以上代码中的 YourServiceReference、YourRemoteMethodAsync、YourRequestData、YourResultType 等都是示例,请根据实际情况替换为您的实际服务引用和方法名称。另外,HTTP 客户端方法适用于 RESTful 风格的 Web API,如果服务提供的是 SOAP Web 服务,请使用 Service References 方法来调用。

相关推荐
JavaGuide1 小时前
Gemini 杀疯了,反重力给我用爽了!分享 8 个 Antigravity 使用技巧
人工智能·后端
handsome_sai1 小时前
《Linux 小白查看系统与软件商店指南》
linux·运维·服务器
Rabitebla2 小时前
【Linux】 文件系统(五):挂载与软硬链接,多个文件系统怎样组成统一目录树
linux·运维·服务器·c++·笔记·学习·算法
数字新视界2 小时前
动环智能监控历史数据留存与故障回溯方法
运维·服务器·网络·机房·模块化机房
星栈2 小时前
我在浏览器里试了试 PhotoCraft|Rust + WASM 修图,是玩具还是生产力?
前端
想不到一个好的ID2 小时前
Redis 哨兵(Sentinel)
后端
皓月盈江2 小时前
云服务器上部署Hexo静态博客
linux·服务器·hexo·个人博客·云服务器·个人网站·静态博客
想不到一个好的ID2 小时前
Redis 高可用:主从复制详解
后端
源码_空间3 小时前
基于Springboot的在线编程练习判题平台(源码+文档)
java·spring boot·后端
雨中飘荡的记忆3 小时前
淘宝搜不到的东西,能不能去全世界找?一个程序员的创业尝试
前端·设计