gpt4 book ai didi

c# - 从 .Net 客户端将图像发送到 SOAP 1.0 网络服务

转载 作者:行者123 更新时间:2023-11-30 21:06:19 25 4
gpt4 key购买 nike

我需要将图像发送到用 PHP 实现的 SOAP 网络服务。

服务的 WSDL 看起来像这样......

<xsd:complexType name="Product">
<xsd:all>
<xsd:element name="ProductId" type="xsd:int"/>
<xsd:element name="Image01" type="xsd:base64Array"/>
</xsd:all>
</xsd:complexType>

当我在我的 C# 应用程序中引用此服务时,用于 Image01 的数据类型是 String

如何从磁盘获取图像并以正确的方式发送编码以通过这种复杂类型发送它?

将不胜感激示例代码。

最佳答案

将图像加载到 byte[] 类型中,然后通过 Convert.ToBase64String() 运行它

有一个很好的代码示例 on this question将文件从磁盘加载到 byte[]

public byte[] StreamToByteArray(string fileName)
{
byte[] total_stream = new byte[0];
using (Stream input = File.Open(fileName, FileMode.Open, FileAccess.Read))
{
byte[] stream_array = new byte[0];
// Setup whatever read size you want (small here for testing)
byte[] buffer = new byte[32];// * 1024];
int read = 0;

while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
stream_array = new byte[total_stream.Length + read];
total_stream.CopyTo(stream_array, 0);
Array.Copy(buffer, 0, stream_array, total_stream.Length, read);
total_stream = stream_array;
}
}
return total_stream;
}

所以你会做

Convert.ToBase64String(this.StreamToByteArray("Filename"));

然后通过网络服务调用传回。我避免使用 Image.FromFile 调用,因此您可以将此示例与其他非图像调用一起重复使用,以通过网络服务发送二进制信息。但是,如果您只想使用图像,则将此代码块替换为 Image.FromFile() 命令。

关于c# - 从 .Net 客户端将图像发送到 SOAP 1.0 网络服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11273206/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com