Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
1.67/5 (2 votes)
See more:
Hi all,

I hope someone can help. I keep getting 'Internal Server Error 500' with this code. I'm starting to go insane.

Here is code:

C#
public static void getAuthenticationToken()
{
  StringBuilder xml = new StringBuilder();
  xml.Append(@"<?xml version=""1.0"" encoding=""utf-8""?>");
  xml.Append(@"<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" ");
  xml.Append(@"xmlns:ser=""http://www.UKMail.com/Services/Contracts/ServiceContracts"" ");
  xml.Append(@"xmlns:dat=""http://www.UKMail.com/Services/Contracts/DataContracts"">");
  xml.Append("<soapenv:Header/>");
  xml.Append("<soapenv:Body>");
  xml.Append("<ser:Login>");
  xml.Append("<ser:loginWebRequest>");
  xml.Append("<dat:Password>password</dat:Password>");
  xml.Append("<dat:Username>username</dat:Username>");
  xml.Append("</ser:loginWebRequest>");
  xml.Append("</ser:Login>");
  xml.Append("</soapenv:Body>");
  xml.Append("</soapenv:Envelope>");

  string s = getUKMailData(xml.ToString(), "https://qa-api.ukmail.com/Services/UKMAuthenticationServices/UKMAuthenticationService.svc?wsdl");
}

public static void getUKMailData(string xml, string address)
{           
   string result = "";
   HttpWebRequest request = CreateWebRequest(address);
   XmlDocument soapEnvelopeXml = new XmlDocument();
   soapEnvelopeXml.LoadXml(xml);

   using (Stream stream = request.GetRequestStream())
   {
      soapEnvelopeXml.Save(stream);
   }

   using (WebResponse response = request.GetResponse()) // Error occurs here
   {
      using (StreamReader rd = new StreamReader(response.GetResponseStream()))
      {
         string soapResult = rd.ReadToEnd();
         Console.WriteLine(soapResult);
      }
   }
}

public static HttpWebRequest CreateWebRequest(string url)
{
   HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
   webRequest.Headers.Add("SOAP:Action");
   webRequest.ContentType = "text/xml;charset=\"utf-8\"";
   webRequest.Accept = "text/xml"; 
   webRequest.Method = "POST";
   return webRequest;
}



I'm new to SOAP and I'm not sure how to get it to work in .NET. Any help would be appreciated.
Posted
Updated 30-Nov-18 10:57am
Comments
Sergey Alexandrovich Kryukov 14-May-14 14:13pm    
Do you think you are just starting? Want to talk about it? :-)
—SA

It's amazing to see that you are trying to manually compose XML text, not even using any of the XML types available in .NET. Why not using .NET FCL types designed for such purposes? Please see:
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.formatters.soap.soapformatter%28v=vs.110%29.aspx[^].

And I don't know if you are developing the service itself as well. If you do, it would be more appropriate to redefine it using much higher levels of abstractions, such as Data Contract or Service Contract. This would be a whole different story. For now, I would advise to read what the standard libraries offer for you, starting from .NET BCL.

—SA
 
Share this answer
 
you may have already fix the issue with the request, but why all these work? you can simply add service reference to your project by giving the WCF url. then it will generate client proxy class for the service. then you can call the methods like below

C#
UKMAuthenticationServiceClient client = new UKMAuthenticationServiceClient();
var response  = client.Login(new LoginWebRequest() { Password = "??", Username = "??" });


How to: Add, Update, or Remove a Service Reference[^]
 
Share this answer
 
v2
Comments
Member 10194820 14-May-14 23:45pm    
That sounds much better, I'll look into it thanks.
The problem was
C#
webRequest.Headers.Add("SOAP:Action");


I changed it to:

C#
webRequest.Headers.Add("SOAPAction", action);


action = "http://www.UKMail.com/Services/IUKMAuthenticationService/Login";
 
Share this answer
 
Comments
mo.ibm 22-Feb-23 4:22am    
Awesome. You saved my day
Hi!

I had the same error and solved it in the method "CreateWebRequest", it happens that you have to add other property the "webRequest.Headers["Authorization"]"

public static HttpWebRequest CreateWebRequest(string url)
{
authInfo =
"Basic " + Convert.ToBase64String(Encoding.Default.GetBytes("username:password"));
<------
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Headers.Add("SOAPAction",
serviceUrl + @"/" + method
);
webRequest.Headers["Authorization"] = authInfo ;
<--------
webRequest.ContentType = "text/xml;charset="UTF-8"";
webRequest.Accept = "text/xml";
webRequest.Method = "POST";
return webRequest;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900