Creating HTTP Header parameters in a SOAP interface in .Net POST or Get

Markus Freitag 3,786 Reputation points
2022-01-25T16:55:42.303+00:00

Hello,
//Visual Studio 2017, C#

When I add a web reference from the WSDL it will generate a class for you that you use to make the calls to your service.
Let's call it "MyService". If I create a partial for that class, and include it in the same assembly,
I can override the "GetWebRequest" method and directly add headers.

Works well.
First request --> IO
Second request --> NIO

Exception.  
catch (Exception ex)    //########## --> here is the exception  
{   
	string error = ex.Message;  
	string stackTrace = ex.StackTrace;  
}  

When I wait more than 3 seconds works too.

Change from Get to POST, nothing happen. The same.

Does anyone have a good solution? Does anyone know a possible cause?

I think the request is empty, why?

The request content type found by the client is 'text/html; charset=iso-8859-1', was expected 'text/xml'.
Error message
--
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">

creating-http-header-parameters-in-a-soap-interface-in-net

protected override WebRequest GetWebRequest(Uri uri)  
{  
//https://stackoverflow.com/questions/28239214/creating-http-header-parameters-in-a-soap-interface-in-net  
  
	HttpWebRequest request;  
	request = (HttpWebRequest)base.GetWebRequest(uri);  
	if (PreAuthenticate)  
	{  
		NetworkCredential networkCredentials =  
			Credentials.GetCredential(uri, "Basic");  
  
		if (networkCredentials != null)  
		{  
			byte[] credentialBuffer = new UTF8Encoding().GetBytes(  
				networkCredentials.UserName + ":" +  
				networkCredentials.Password);  
  
			SetRequestHeader("Authorization", "Basic " + Convert.ToBase64String(credentialBuffer));  
			SetRequestHeader("Accept-Encoding", "gzip,deflate");  
  
  
			var httpRequest = request as HttpWebRequest;  
			if (httpRequest != null)  
			{  
				foreach (string headerName in this.requestHeaders.Keys)  
				{  
					httpRequest.Headers[headerName] = this.requestHeaders[headerName];  
				}  
			}  
			request.ContentType = "text/xml;charset=\"utf-8\"";  
		  //  request.Accept = "text/xml";  
		  //  request.Method = "POST";		    
  
			return request;  //########## --> here is the exception  
Windows Server
Windows Server
A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.
13,239 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,023 questions
0 comments No comments
{count} votes

Accepted answer
  1. AgaveJoe 28,536 Reputation points
    2022-01-25T21:10:39.787+00:00

    Fundamentally an HTTP GET submits data in the URL. An HTTP POST submits data in the message body. You cannot switch a GET to a POST without adding the POST message body. Since this is a SOAP request the message body is SOAP/XML.

    Secondly, the code you shared is not code generated form a WSDL.

    Contact the service owners for support. Perhaps they allow a standard HTML form post since, if I understand correctly, they allow HTTP GET. the service might allow content-type: application/x-www-form-urlencoded or maybe JSON. The service documentation should cover the details.

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. AgaveJoe 28,536 Reputation points
    2022-01-26T21:00:57.903+00:00

    In one of your other threads, for which there are many on this subject, I provided tested sample code that adds a basic authentication header using the proxy class created by the wsdl.exe utility. Below is a copy of the code for your reference.

    static void Main(string[] args)
    {
        WebService client = new WebService()
        {
            Credentials = new NetworkCredential("username", "password"),
            PreAuthenticate = true,
            Url = "http://localhost/WebService/WebService.asmx"
        };
    
        string resposne = client.HelloWorld();
        Console.WriteLine(resposne);
    }
    

    The sample sends a Content-Type: text/xml; charset=utf-8 header. There is no need to add this header manually.

    Example HTTP capture from the code above.

    POST http://mgebhardt-p4879/WebService/WebService.asmx HTTP/1.1
    User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; MS Web Services Client Protocol 4.0.30319.42000)
    Content-Type: text/xml; charset=utf-8
    SOAPAction: "http://tempuri.org/HelloWorld"
    Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=
    Host: localhost
    Content-Length: 288
    Expect: 100-continue
    
    <?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><HelloWorld xmlns="http://tempuri.org/" /></soap:Body></soap:Envelope>
    

    My best guess is the SOAP service you are calling does not use basic authentication. Reach out to the service owners, as recommend countless times, to learn how the service works. Your current approach of aimlessly trying code found on the Internet is clearly not working.

    1 person found this answer helpful.
    0 comments No comments

  2. Limitless Technology 39,686 Reputation points
    2022-02-01T09:45:30.473+00:00

    Hi there,

    When you add a web reference from the WSDL it will generate a class for you that you use to make the calls to your service, let's call it "MyService". If you create a partial for that class and include it in the same assembly, you can override the "GetWebRequest" method and directly add headers.

    You could try these codes:

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create( Your URL );
    request.Headers.Add( "SOAPAction", YOUR SOAP ACTION );

    Here is a thread as well that discusses the same topics and might be helpful.

    How to add http header to webservice soap request C#
    https://learn.microsoft.com/en-us/answers/questions/480484/how-to-add-http-header-to-webservice-soap-request.html

    Adding information to the SOAP Header
    https://social.msdn.microsoft.com/Forums/en-US/f64cd3b9-2083-4bc4-be7b-846aee42442e/adding-information-to-the-soap-header?forum=aspwcfasmx

    ----------------------------------------------------------------------------------------------------------

    --If the reply is helpful, please Upvote and Accept it as an answer--

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.