【问题标题】:adding a soap header to a web service reference in c#在 C# 中将soap标头添加到Web服务引用
【发布时间】:2014-11-03 22:10:41
【问题描述】:

我已经通过在 Visual Studio 2012 C# 中添加服务引用来添加 WSDL 文件,并且已经添加了一个代理类,我可以使用有问题的 Web 服务。 问题是我需要根据软件信息在soap头中传递一些额外的值,例如:

    POST /uondevws/Dashboard.asmx HTTP/1.1
Host: uondev.bluera.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/GetAllProjectMetaData"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <APIKeyHeader xmlns="http://tempuri.org/">
      <Value>string</Value>
    </APIKeyHeader>
    <Message xmlns="http://tempuri.org/">
      <Value>string</Value>
    </Message>
  </soap:Header>
  <soap:Body>
    <GetAllProjectMetaData xmlns="http://tempuri.org/" />
  </soap:Body>
</soap:Envelope>

但是由于我已经添加了 WSDL 文件,我不需要重新构建soap 消息,我只需要使用相关服务即可。在值内部将是能够调用 API 的关键 这是我的代码:所有这些都是在一个按钮内的 Windows 窗体中完成的,所以当我单击该按钮时,我可以显示 foreach 函数内的所有字段

    namespace GetAllMetaDataApplication
{

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        public void button1_Click(object sender, EventArgs e)
        {

            test.Dashboard proxy = new test.Dashboard();


            test.ProjectMetaData[] nc = test.GetAllProjectMetaData();
            proxy.APIKeyHeaderValue = new test.APIKeyHeader();
            proxy.APIKeyHeaderValue.Value = "ba97e6a9-3b6d-40ac";

     StringBuilder sb = new StringBuilder();


            foreach (test.ProjectMetaData som in nc)
            {
                sb.AppendLine("\r\n");
                sb.AppendLine("\r\n" + som.ProjectTitle + "       " + som.ProjectID + "       " + som.PublishStatus);

            }
            //StringBuilder.StringBuilder();
            label1.Text = sb.ToString(); 
        }
    }

我的问题是什么都没有显示,每当我点击表单按钮时什么都没有发生,你能告诉我我错过了什么吗?

谢谢

【问题讨论】:

    标签: c# winforms visual-studio-2012 proxy wsdl


    【解决方案1】:

    您在调用 GetAllProjectMetaData() Web 服务方法后设置 APIKeyHeaderValue。你应该颠倒顺序:

        proxy.APIKeyHeaderValue = new uondev.APIKeyHeader();
        proxy.APIKeyHeaderValue.Value = "ba97e6a9-3b6d-40ac";
        test.ProjectMetaData[] nc = test.GetAllProjectMetaData();
    

    如果您仍然有问题,您可以使用 Fiddler(http://www.telerik.com/fiddler) 查看已通过网络传输的消息。

    【讨论】:

    • @user3790916 如果回答了您的问题,请将其标记为答案。
    猜你喜欢
    • 1970-01-01
    • 2020-12-06
    • 1970-01-01
    • 1970-01-01
    • 2011-01-12
    • 2013-01-29
    • 2020-10-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多