简体   繁体   中英

Call webservice from sql stored procedure

I want to call a webservice from my stored procedure and get an XML answere. Im don't realy know how to do this since i haven't done anything like this before.

This is what my stored procedure looks like:

   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

For now i get this error: Server was unable to process request. ---> Root element is missing

I just don't know what to do from this or what i am doing wrong here.

If i try to run the url i get this:

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>

You need to do two things:

  1. Your url must look like: ' http://79.171.249.41/nasherpopman/PopManWebService.asmx/GetPerson?personnumber= ' + @personnr +'&...
  2. Then you should enable POST and GET protocol using your Web Service config file.
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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM