C#接口文件传输WebService

一.需求

二个系统接口对接中,其中一方需要进行文件传输,另一方接受保存。

二.思路

首先了解接口接受的参数类型, 方式选择以文件流的形式打开文件,转换为byte进行存储,然后通过接口传送,对方接受后存在目录中即可。

三.解决办法

1.基础环境

1.因为是WebService的项目那么首先看对方的接口接受的参数是说明类型
2.先添加服务引用选择高级,在兼容性中点击添加web引用
在这里插入图片描述
3.输入地址URL命名web引用名,点击添加引用
在这里插入图片描述
4.将会多出一个文件夹
在这里插入图片描述

2.代码部分
一.打开文件转换成文件流
        WebService aa = new WebService();
        //打开文件
        FileStream fileStream = new FileStream("C:/MNWEB/image/1554965171(1).jpg", FileMode.Open, FileAccess.Read, FileShare.Read); 
        byte[] bytes = new byte[fileStream.Length];
        fileStream.Read(bytes, 0, bytes.Length);
        //关闭文件流
        fileStream.Close();
        //调用发起
        aa.filemanery(bytes);
二.接受部分
   [WebMethod]
        public void filemanery(byte[] bytes) {
            // 把 byte[] 写入文件
            FileStream fs = new FileStream("E:/LIMS/LIMS1.0/WebAppointmentToLIMS/WebAppointmentToLIMS/file/1554965171(1).jpg", FileMode.Create);

            BinaryWriter bw = new BinaryWriter(fs);
进行写入
            bw.Write(bytes);
关闭写功能
            bw.Close();
关闭流
            fs.Close();
        }
        ```
  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
当你要调用一个 WebService 接口时,你可以按照以下步骤进行: 1. 创建一个 C# 项目或打开一个现有的项目。 2. 在项目中引用 System.Web.Services 命名空间。 3. 添加对 WebService 的引用。你可以在 Visual Studio 的 "项目资源管理器" 窗口中右键点击 "引用",然后选择 "添加服务引用"。在弹出窗口中,输入 WebService 的 URL 地址,然后点击 "Go" 按钮。选择你想要添加的 WebService,点击 "确定"。 4. Visual Studio 会自动生成一个代理类,该类包含对 WebService 方法的调用。 以下是一个示例代码,展示了如何调用一个 WebService 的方法: ```csharp using System; using System.Web.Services; namespace WebServiceClient { class Program { static void Main(string[] args) { // 创建 WebService 代理类实例 MyWebServiceProxy webServiceProxy = new MyWebServiceProxy(); try { // 调用 WebService 方法并打印结果 string result = webServiceProxy.HelloWorld(); Console.WriteLine(result); } catch (Exception ex) { Console.WriteLine("调用失败: " + ex.Message); } } } // WebService 代理类 [WebServiceBinding(Name = "MyWebService", Namespace = "http://www.example.com/")] public class MyWebServiceProxy : SoapHttpClientProtocol { // WebService 方法的声明 [WebMethod] public string HelloWorld() { return this.Invoke("HelloWorld", new object[] { }) as string; } } } ``` 上面的代码假设 WebService 的命名空间是 "http://www.example.com/",并且包含一个名为 "HelloWorld" 的方法。你需要将这些信息替换为你实际使用的 WebService 的命名空间和方法名称。 请注意,如果你的 WebService 需要身份验证等额外步骤,请在调用之前执行必要的身份验证操作。具体步骤可能因 WebService 的要求而有所不同。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

因为有你更精彩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值