Click here to Skip to main content
15,885,998 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi friends,
I am calling webservice from classic asp.
and i am able to call the service but not able to get the correct output.
i am geting following error.
error:-
VB
soap:ReceiverSystem.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.Xml.XmlException: Data at the root level is invalid. Line 1, position 1. at System.Xml.XmlTextReaderImpl.Throw(Exception e) at System.Xml.XmlTextReaderImpl.Throw(String res, String arg) at System.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace() at System.Xml.XmlTextReaderImpl.ParseDocumentContent() at System.Xml.XmlTextReaderImpl.Read() at System.Xml.XmlTextReader.Read() at System.Web.Services.Protocols.SoapServerProtocol.SoapEnvelopeReader.Read() at System.Xml.XmlReader.MoveToContent() at System.Web.Services.Protocols.SoapServerProtocol.SoapEnvelopeReader.MoveToContent() at System.Web.Services.Protocols.SoapServerProtocolHelper.GetRequestElement() at System.Web.Services.Protocols.Soap12ServerProtocolHelper.RouteRequest() at System.Web.Services.Protocols.SoapServerProtocol.RouteRequest(SoapServerMessage message) at System.Web.Services.Protocols.SoapServerProtocol.Initialize() at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing) --- End of inner exception stack trace --- 


below is my asp code to call websrvice..

HTML
<%
If Request.ServerVariables("REQUEST_METHOD") = "POST" Then
Dim xmlhttp
Dim DataToSend
DataToSend="val1="&Request.Form("text1")&"&val2="&Request.Form("text2")
Dim postUrl
If Request.Form.Item("Operation")="Sum" Then
postUrl = "http://100.10.10.149/webservice/Service1.asmx?op=sum"
else
postUrl = "http://100.10.10.149/webservice/Service1.asmx?op=Subtract" 
end if 
'set xmlhttp = Server.CreateObject("msxml2.ServerXMLHTTP") 
'set xmlhttp = Server.CreateObject("Microsoft.XMLHTTP") 
'set xmlhttp = Server.CreateObject("MSXML2.DOMDocument")
'Set xmlhttp = Server.CreateObject("MSXML2.ServerXMLHTTP.4.0") 

Set xmlhttp = server.Createobject("MSXML2.XMLHTTP")
xmlhttp.Open "POST",postUrl,false
xmlhttp.setRequestHeader "Content-Type","application/x-www-form-urlencoded"
'Response.Write postUrl
'Response.End
xmlhttp.send DataToSend

Response.Write DataToSend & "<br>"
Response.Write(xmlhttp.responseText )
Else
Response.Write "Loading for first Time" 
End If
%>


and in my webservice there are only two webmethods. sum & substract..as below
C#
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;

namespace WebService1
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [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 Service1 : System.Web.Services.WebService
    {

        [WebMethod]
        public string Sum(int val1, int val2)
        {
            return "The Sum of two number= " + (val1 + val2);
        }
        [WebMethod]
        public string Subtract(int val1, int val2)
        {
            return "The Subtraction of two number= " + ((val1 > val2) ? (val1 - val2) : (val2 - val1));
        }

    }
}


your help would be appreciable..
Posted

Hello friends,
i have solved this issue...
please see my code for SUm method..

HTML
 Dim xmlhttp
 Dim DataToSend
 DataToSend="Val1="&Request.Form("text1")&"&Val2="&Request.Form("text2")
 Dim postUrl
 postUrl = "http://100.10.10.149/webservice/Service1.asmx/Sum"
Set xmlhttp = server.Createobject("MSXML2.XMLHTTP")
 xmlhttp.Open "POST",postUrl,false
 xmlhttp.setRequestHeader "Content-Type","application/x-www-form-urlencoded"
 xmlhttp.send DataToSend
 Response.Write(xmlhttp.responseText)


and the main thing is below code needs to add in webservice's web.confg file.

XML
<webservices>
     <protocols>
       <add name="HttpGet" />
       <add name="HttpPost" />
     </protocols>
   </webservices>


Thanks for all replys... :)
 
Share this answer
 
System.Xml.XmlException: Data at the root level is invalid. Line 1, position 1
See if your XML file is properly formed. Make sure there is no space or new line at the top before the root tag.

Stack trace says, webservice was expecting request in form of a valid XML but request did not had a valid xml.

Did you DEBUG and see what was sent? Fiddler?

Sending a method as a querystring and using XMLHttpRequest would work only for methods with no parameters.
For accessing methods with parameters, you need to pass the URL with method name and paratemer as query string.
Following article illustrates it: Consuming .NET Web Services Using the XMLHTTP Protocol[^]


Here have a look at this another article on similar scenario: Calling a WebService from ASP3.0 and JavaScript[^]
 
Share this answer
 
v2
Comments
ssd_coolguy 10-Jul-12 2:50am    
thanks for reply... your links helped me lot... :)
i am about to solve this problem..:) soon i will update answer here..
Sandeep Mewara 10-Jul-12 5:48am    
Good to know.

Prasad_Kulkarni 10-Jul-12 6:36am    
Good answer! +5
Sandeep Mewara 13-Jul-12 3:27am    
Thanks PK.

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