I have created a web service. Unfortunately, I am not well versed with SOAP call, however I don’t think I am far off. The web service shown below has 3 test endpoints which I am able to call without any issue, however when I try to call InsertTx5xxCO2Sample
, I am getting an error:
Request format is invalid: text/xml
I am not sure what I am doing wrong with this one endpoint.
Web service: UserService.asmx
:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
namespace SOAPWebServiceProject
{
/// <summary>
/// Summary description for UserService
/// </summary>
[WebService(Namespace = "http://cometsystem.cz/schemas/soapTx5xxCO2.xsd")]
[SoapDocumentService(RoutingStyle = SoapServiceRoutingStyle.RequestElement)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class UserService : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
[WebMethod]
public string GetUserDetails(int userId)
{
// Example logic to fetch user details from a database
if (userId == 1)
return "User: John Doe, Email: [email protected]";
else
return "User not found";
}
[WebMethod]
public string TestExml(string testexl)
{
List<string> workingDir = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase).Split('\').ToList();
workingDir.RemoveAt(0);
workingDir.RemoveAt(workingDir.Count - 1);
string path = string.Join(@"", workingDir);
File.WriteAllText(path + @"Test.txt", "Hello world");
return "Hello World";
}
[WebMethod]
public bool InsertTx5xxCO2Sample(string passKey, string device,
string temp, string relHum,
string compQuant, string co2,
string alarms, string lev1,
string lev2, string lev3,
string compType, string tempU,
string timer)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine(string.Format("passKey: {0}", passKey));
sb.AppendLine(string.Format("device: {0}", device));
sb.AppendLine(string.Format("temp: {0}", temp));
sb.AppendLine(string.Format("relHum: {0}", relHum));
sb.AppendLine(string.Format("compQuant: {0}", compQuant));
sb.AppendLine(string.Format("co2: {0}", co2));
sb.AppendLine(string.Format("alarms: {0}", alarms));
sb.AppendLine(string.Format("lev1: {0}", lev1));
sb.AppendLine(string.Format("lev2: {0}", lev2));
sb.AppendLine(string.Format("lev3: {0}", lev3));
sb.AppendLine(string.Format("compType: {0}", compType));
sb.AppendLine(string.Format("tempU: {0}", tempU));
sb.AppendLine(string.Format("timer: {0}", timer));
List<string> workingDir = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase).Split('\').ToList();
workingDir.RemoveAt(0);
workingDir.RemoveAt(workingDir.Count - 1);
string path = string.Join(@"", workingDir);
if (File.Exists(path + @"Test2.txt"))
File.Delete(path + @"Test2.txt");
File.WriteAllText(path + @"Test2.txt", sb.ToString());
return true;
}
}
}
Web service web.config
:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.8" />
<httpRuntime targetFramework="4.8" />
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
</system.web>
<system.webServer>
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Antlr3.Runtime" publicKeyToken="eb42632606e9261f" />
<bindingRedirect oldVersion="0.0.0.0-3.5.0.2" newVersion="3.5.0.2" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
<bindingRedirect oldVersion="0.0.0.0-13.0.0.0" newVersion="13.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Web.Infrastructure" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-2.0.1.0" newVersion="2.0.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-1.6.5135.21930" newVersion="1.6.5135.21930" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-5.2.9.0" newVersion="5.2.9.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" />
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE="Web" /optionInfer+" />
</compilers>
</system.codedom>
</configuration>
Call:
POST http://ipsum:5854/UserService.asmx/InsertTx5xxCO2Sample HTTP/1.1
Host: ipsum:5854
Content-Type: text/xml
Content-Length: 656
<?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>
<InsertTx5xxCO2Sample xmlns="http://cometsystem.cz/schemas/soapTx5xxCO2.xsd">
<passKey>16963997</passKey>
<device>4158</device>
<temp>24.7</temp>
<relHum>35.9</relHum>
<compQuant>8.6</compQuant>
<co2>510</co2>
<alarms>hi,lo,no,no</alarms>
<lev1>0</lev1>
<lev2>0</lev2>
<lev3>1</lev3>
<compType>Dew point</compType>
<tempU>C</tempU>
<timer>10</timer>
</InsertTx5xxCO2Sample>
</soap:Body>
</soap:Envelope>
Response:
HTTP/1.1 500 Internal Server Error
Cache-Control: private
Content-Type: text/plain; charset=utf-8
Server: Microsoft-IIS/8.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Thu, 18 Jul 2024 18:34:24 GMT
Content-Length: 38
Request format is invalid: text/xml.