通过经典ASP调用Web服务时出错. [英] getting error while calling webservice through classic ASP.

查看:95
本文介绍了通过经典ASP调用Web服务时出错.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友们,
我正在从classic asp.
调用Web服务 而且我可以致电服务,但无法获得正确的输出.
我遇到以下错误.
错误:-

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:-

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 --- 



下面是我的asp代码来调用websrvice ..



below is my asp code to call websrvice..

<%
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

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..

推荐答案

System.Xml.XmlException:根级别的数据无效.第1行,位置1
查看您的XML文件格式是否正确.确保根标记之前的顶部没有空格或换行.

堆栈跟踪说,webservice期望请求以有效XML的形式出现,但是请求没有有效的xml.

您是否已调试并查看发送的内容?小提琴手?

将方法作为查询字符串发送并使用XMLHttpRequest仅适用于没有参数的方法.
要访问带有参数的方法,您需要传递带有方法名称和参数的URL作为查询字符串.
下一篇文章对此进行了说明:使用XMLHTTP协议使用.NET Web服务 [ ^ ]


在这里,您可以看一下另一篇类似情况的文章:调用a来自ASP3.0和JavaScript的WebService [
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[^]


朋友您好,
我已经解决了这个问题...
请参阅我的SUm方法代码..

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

 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)



而且最主要的是,下面的代码需要添加到webservice的web.confg文件中.



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

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



感谢您的所有回复...:)



Thanks for all replys... :)


这篇关于通过经典ASP调用Web服务时出错.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆