在c#中为SOAP服务引用添加soap标头

时间:2014-11-03 22:10:42

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

我通过在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(); 
        }
    }

我的问题是没有显示任何内容,每当我点击表单按钮时没有任何反应,你能不能让我知道我缺少什么?

由于

1 个答案:

答案 0 :(得分: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)查看通过网络传输的消息。

相关问题