从sql存储过程调用webservice

时间:2014-03-11 08:33:53

标签: sql web-services stored-procedures sql-server-2012

我想从我的存储过程调用web服务并获得XML回复。我真的不知道如何做到这一点,因为我之前没有做过这样的事情。

这就是我的存储过程:

   USE [master]
GO
/****** Object:  StoredProcedure [dbo].[webServiceCall]    Script Date: 3/11/2014     9:12:49 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[webServiceCall] 
    -- Add the parameters for the stored procedure here
    @personnr varchar (20),
    @username varchar (20),
    @password varchar (20)

AS
 DECLARE @obj INT
 DECLARE @ValorDeRegreso INT
 DECLARE @hr INT
 DECLARE @src varchar(255)
 DECLARE @desc varchar (255)
 DECLARE @srv varchar (200)
 DECLARE @response varchar (8000)



SET @srv = 'http://..../nasherpopman/PopManWebService.asmx?op=GetPerson&personnumber=' + @personnr +'&username=' + @username + '&password=' + @password +''
EXEC sp_OACreate 'MSXML2.ServerXMLHttp', @obj OUT
EXEC sp_OAMethod @obj, 'open', NULL, 'POST', @srv, false
EXEC sp_OAMethod @obj, 'send'
EXEC sp_OAGetProperty @obj, 'responseText', @response out
SELECT @response [response]
EXEC sp_OADestroy @obj
RETURN

现在我收到此错误:服务器无法处理请求。 ---&GT;根元素缺失

我只是不知道该怎么做或我在这里做错了什么。

如果我尝试运行网址,我会得到这个:

GetPerson

Test

The test form is only available for requests from the local machine.
SOAP 1.1

The following is a sample SOAP 1.1 request and response. The placeholders shown need to     be replaced with actual values.

POST /nasherpopman/PopManWebService.asmx HTTP/1.1
Host: .....
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/GetPerson"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns:xsd="http://www.w3.org/2001/XMLSchema"     xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <GetPerson xmlns="http://tempuri.org/">
      <personnumber>string</personnumber>
      <username>string</username>
      <password>string</password>
    </GetPerson>
  </soap:Body>
</soap:Envelope>
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <GetPersonResponse xmlns="http://tempuri.org/">
      <GetPersonResult>

      <personResult>string</personResult>
    </GetPersonResponse>
  </soap:Body>
</soap:Envelope>

2 个答案:

答案 0 :(得分:1)

你需要做两件事:

  1. 您的网址必须如下:'http://79.171.249.41/nasherpopman/PopManWebService.asmx/GetPerson?personnumber='+ @personnr +'&amp; ...
  2. 然后,您应该使用Web服务配置文件启用POST和GET协议。

答案 1 :(得分:0)

USE [CalculateDistance]
GO
/****** Object:  StoredProcedure [dbo].[CalculateDistence]    Script Date: 12/1/2014 12:49:42 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      <Author,,Rajay Sachdeva>
-- Create date: <Create 25/11/2014,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[CalculateDistence] --'','','','37064','','','','78701',''
(
@ToAddress varchar(100) =null,
@ToCity varchar(100)=null,
@ToState varchar(100)=null,
@ToPostCode varchar(100)=null,
@FromAddress varchar(100)=null,
@FromCity varchar(100)=null,
@FromState varchar(100)=null,
@FromPostCode varchar(100)=null,
@DistanceInKilometers varchar(100) output
)
AS

     Declare @Object as Int;
Declare @ResponseText as Varchar(8000);
Declare @serviceUrl as varchar(500)
  set @serviceUrl = 'http://maps.googleapis.com/maps/api/distancematrix/xml?origins=' +@ToAddress+@ToCity+@ToState+@ToPostCode+
   '&destinations=' +@FromAddress+@FromCity+@FromState+ @FromPostCode +'&mode=driving&language=en-EN&units=metric;'
Exec sp_OACreate 'MSXML2.XMLHTTP', @Object OUT;
Exec sp_OAMethod @Object, 'open', NULL, 'get',
                @serviceUrl, --Your Web Service Url (invoked)
                 'false'
Exec sp_OAMethod @Object, 'send'
Exec sp_OAMethod @Object, 'responseText', @ResponseText OUTPUT

Declare @Response as XML

--Select @ResponseText as XMLList

SET @Response = CAST(@ResponseText AS XML);

  Declare @Status as varchar(20)
   Declare @Distance as varchar(20)


  set @Status= @Response.value('(DistanceMatrixResponse/row/element/status)[1]', 'varchar(20)') 
  print @Status
  if(@Status='ZERO_RESULTS')

   Begin

                     set @Distance=@Status
   End
   else

   Begin
   set @Distance=@Response.value('(DistanceMatrixResponse/row/element/distance/text)[1]', 'varchar(20)') 
   End
  --Select @Response.value('(DistanceMatrixResponse/row/element/distance/text)[1]', 'varchar(10)') as Distance 
  select @Distance as Distance
Exec sp_OADestroy @Object