c# - 在 PHP 中通过 SOAP 发送 Zip 文件

标签 c# php web-services soap wsdl

我必须将文件发送到 WSDL,该元素在 WSDL 中描述为:

<s:element minOccurs="0" maxOccurs="1" name="theZipFile" type="s:base64Binary" />

如何使用 SOAP 客户端发送 Zip 文件?我尝试了以下方法:

$client = new SoapClient($url);
$params = array("theZipFile" => "file.zip");
$response = $client->theFunction($params);

但我没有得到预期的响应。我尝试使用 .Net 和 C# 以及以下代码:

string filename = "file.zip";
FileInfo fi = new FileInfo(filename);
long numBytes = fi.Length;
FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
byte[] data = br.ReadBytes((int)numBytes);
br.Close();
fs.Close();

XElement response = client.theFunction(data);

而且它没有任何问题。

谢谢!

最佳答案

由于某些奇怪的原因,SoapClient 没有发送正确的 XML,显然是定义有问题。

改为使用 CURL。

function SOAPRawRequest($url, $postString, &$error) {
  $soap_do = curl_init(); 
  curl_setopt($soap_do, CURLOPT_URL,            $url );   
  curl_setopt($soap_do, CURLOPT_CONNECTTIMEOUT, 10); 
  curl_setopt($soap_do, CURLOPT_TIMEOUT,        10); 
  curl_setopt($soap_do, CURLOPT_RETURNTRANSFER, true );
  curl_setopt($soap_do, CURLOPT_SSL_VERIFYPEER, false);  
  curl_setopt($soap_do, CURLOPT_SSL_VERIFYHOST, false); 
  curl_setopt($soap_do, CURLOPT_POST,           true ); 
  curl_setopt($soap_do, CURLOPT_POSTFIELDS,    $postString); 
  curl_setopt($soap_do, CURLOPT_HTTPHEADER,     
    array('Content-Type: text/xml; charset=utf-8', 
      "Accept: text/xml",
      "Cache-Control: no-cache",
      "Pragma: no-cache",
      "SOAPAction: \"http://tempuri.org/theFunction\"",
      'Content-Length: '.strlen($postString)
    ));

  $result = curl_exec($soap_do);
  $error = curl_error($soap_do);

  return $result;
}

还将 $params = array("theZipFile"=> "file.zip"); 更改为:

$content = file_get_contents("file.zip");
$content64 = base64_encode($content);

关于c# - 在 PHP 中通过 SOAP 发送 Zip 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28733428/

相关文章:

ios - swift 中的 MVC 结构

c# - 在 MVVM 应用程序中使用 autofac

c# - 从 webBrowser 控件读取 XML

Php Mass/group 检查值

java - 已在 tomcat 中部署 2 个应用程序时出现邮件问题

java - 在 java eclipse 的 web.xml 中使用 servlets 名称和 servlets-url

c# - 如何清除/删除文件 licenses.licx

c# - 无法在 SQL 中创建程序集 'System.ServiceModel.Internals'

php - 为 codeigniter 的电子邮件收件人设置名称(别名)

php - 如何从 preg_split 结果中删除空数组?