简体   繁体   中英

adding a soap header to a web service reference in c#

I have added the WSDL file by adding a service reference in Visual Studio 2012 C#, and this has been added a proxy class which I can consume the web service in question. The problem is that I need to pass some extras values within a soap header, according to the software information such as:

    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>

But since I have added the WSDL file I dont need to re build the soap message, I just need to consume the service in question. Inside the Value ill be the key in question to be able to call the API Here it's my code: All this is done in a Windows form inside a button, so When I click the button I can display all the fields inside the foreach function

    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(); 
        }
    }

My problem is that nothing is displayed, whenever I click on the form button nothing happens, can you please give me an idea what I am missing?

Thanks

You are setting the APIKeyHeaderValue after you call GetAllProjectMetaData() web service method. You should reverse the order:

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

If you are still having problem, you can use Fiddler( http://www.telerik.com/fiddler ) to see what messages have been transferred over the wire.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM