[更新]我不认为这是重复的,因为有问题的帖子是使用MyServiceSoapClient()而我不是。
我正在使用Visual Studio 2017中的C#编写SOAP Web服务调用。
自定义供应商的示例代码我能够执行报告,然后将生成的文件流式传输到本地文件系统。到目前为止,文件是以XML格式下载的,默认情况下。
如果我向SOAP信封添加自定义HTTP标头,我可以修改报告的格式。我需要发送以下内容:
<headers>
<US-DELIMITER>|</US-DELIMITER>
</headers>
然而,我不能为我的生活弄清楚如何做到这一点。我相信我可以通过编程方式或通过在App.config文件中添加内容来实现。我宁愿改变后者。
我的App.Config看起来像这样:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_IBIStreamService" maxReceivedMessageSize="2000000">
<security mode="Transport">
<transport clientCredentialType="None" />
</security>
</binding>
<binding name="WSHttpBinding_IBIDataService" maxReceivedMessageSize="2000000">
<security mode="Transport">
<transport clientCredentialType="None" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="https://service4.ultipro.com/services/BIDataService"
binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IBIDataService"
contract="BIDataService.IBIDataService" name="WSHttpBinding_IBIDataService">
</endpoint>
<endpoint address="https://service4.ultipro.com/services/BIStreamingService"
binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IBIStreamService"
contract="BIStreamingService.IBIStreamService" name="WSHttpBinding_IBIStreamService" />
</client>
</system.serviceModel>
</configuration>
我的代码的重要部分如下所示:
namespace BIWebServiceExample
{
using System;
using System.IO;
using System.ServiceModel;
using UltiProBusinessReports.BIDataService;
using UltiProBusinessReports.BIStreamingService;
class Program
{
private static DataContext Context { get; set; }
private static BIDataServiceClient DataClient { get; set; }
private static String Path { get; set; }
private static String Report { get; set; }
// Entry point of the application
static void Main(string[] args)
{
Path = "XXXXX;
DataClient = new BIDataServiceClient("WSHttpBinding_IBIDataService");
Context = DoAuthenticate();
if (Context.Status == ContextStatus.Ok)
{
Report = "'XXXXX']";
ReportResponse response = ExecuteReport(Context, DataClient, Report);
if (response.Status == ReportRequestStatus.Success)
{
GetReportStreamFromResponse(response, "XXX.csv");
}
else
{
Console.WriteLine(response.StatusMessage);
}
}
else
{
// Inspect the StatusMessage on an authentication failure
Console.WriteLine(Context.StatusMessage);
Console.ReadKey(true);
}
}
private static void GetReportStreamFromResponse(ReportResponse response, String outFile)
{
string msg;
Stream input = null;
BIStreamServiceClient streamClient = null;
try
{
streamClient = new BIStreamServiceClient("WSHttpBinding_IBIStreamService", new EndpointAddress(response.ReportRetrievalUri));
ReportResponseStatus status;
do
{
status = streamClient.RetrieveReport(response.ReportKey, out msg, out input);
} while (status == ReportResponseStatus.Working);
if (status == ReportResponseStatus.Failed)
{
Console.WriteLine("Failed to retrieve report due to \"{0}\"", msg);
return;
}
using (StreamReader reader = new StreamReader(input))
{
using (Stream output = new FileStream("C:\\temp\\" + outFile, FileMode.Create, FileAccess.Write))
{
using (StreamWriter writer = new StreamWriter(output))
{
int bytesRead;
char[] buffer = new char[1024];
while ((bytesRead = reader.Read(buffer, 0, buffer.Length)) > 0)
{
writer.Write(buffer, 0, bytesRead);
}
}
}
}
}
finally
{
if (input != null)
{
input.Close();
}
CloseClientProxy(streamClient);
}
}
private static void CloseClientProxy(ICommunicationObject client)
{}
private static DataContext DoAuthenticate()
{return ThisContext;}
private static ReportResponse ExecuteReport(DataContext context, BIDataServiceClient dataClient, String Report)
{
ReportResponse response =
dataClient.ExecuteReport(
new ReportRequest
{ReportPath = Path + Report
},context);
return response;
}
}
}
非常感谢任何帮助。