Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 http post a xml

Author  Topic 

Hommer
Aged Yak Warrior

808 Posts

Posted - 2006-03-16 : 11:22:26
Hi, everyone!

I am looking for a solution that will send a xml file through http post via a stored procedure.

Does any body have an example on how it can be done?

Thanks!

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2006-03-17 : 04:58:26
try this

create proc spHTTPRequest
@URI varchar(2000) = '',
@methodName varchar(50) = '',
@requestBody varchar(8000) = '',
@UserName nvarchar(100), -- Domain\UserName or UserName
@Password nvarchar(100),
@responseText varchar(8000) output
as
SET NOCOUNT ON
IF @methodName = ''
BEGIN
select FailPoint = 'Method Name must be set'
return
END
set @responseText = 'FAILED'

DECLARE @objectID int
DECLARE @hResult int
DECLARE @source varchar(255), @desc varchar(255)
EXEC @hResult = sp_OACreate 'MSXML2.ServerXMLHTTP.4.0', @objectID OUT
IF @hResult <> 0
BEGIN
EXEC sp_OAGetErrorInfo @objectID, @source OUT, @desc OUT
SELECT hResult = convert(varbinary(4), @hResult),
source = @source,
description = @desc,
FailPoint = 'Create failed',
MedthodName = @methodName
goto destroy
return
END

-- open the destination URI with Specified method
EXEC @hResult = sp_OAMethod @objectID, 'open', null, @methodName, @URI, 'false', @UserName, @Password
IF @hResult <> 0
BEGIN
EXEC sp_OAGetErrorInfo @objectID, @source OUT, @desc OUT
SELECT hResult = convert(varbinary(4), @hResult),
source = @source,
description = @desc,
FailPoint = 'Open failed',
MedthodName = @methodName
goto destroy
return
END

-- set request headers
EXEC @hResult = sp_OAMethod @objectID, 'setRequestHeader', null, 'Content-Type', 'text/xml'
IF @hResult <> 0
BEGIN
EXEC sp_OAGetErrorInfo @objectID, @source OUT, @desc OUT
SELECT hResult = convert(varbinary(4), @hResult),
source = @source,
description = @desc,
FailPoint = 'SetRequestHeader failed',
MedthodName = @methodName
goto destroy
return
END

declare @len int
set @len = len(@requestBody)
EXEC @hResult = sp_OAMethod @objectID, 'setRequestHeader', null, 'Content-Length', @len
IF @hResult <> 0
BEGIN
EXEC sp_OAGetErrorInfo @objectID, @source OUT, @desc OUT
SELECT hResult = convert(varbinary(4), @hResult),
source = @source,
description = @desc,
FailPoint = 'SetRequestHeader failed',
MedthodName = @methodName
goto destroy
return
END

/*
-- if you have headers in a table called RequestHeader you can go through them with this
DECLARE @HeaderKey varchar(500), @HeaderValue varchar(500)
DECLARE RequestHeader CURSOR
LOCAL FAST_FORWARD
FOR
SELECT HeaderKey, HeaderValue
FROM RequestHeaders
WHERE Method = @methodName

OPEN RequestHeader
FETCH NEXT FROM RequestHeader
INTO @HeaderKey, @HeaderValue

WHILE @@FETCH_STATUS = 0
BEGIN
--select @HeaderKey, @HeaderValue, @methodName
EXEC @hResult = sp_OAMethod @objectID, 'setRequestHeader', null, @HeaderKey, @HeaderValue
IF @hResult <> 0
BEGIN
EXEC sp_OAGetErrorInfo @objectID, @source OUT, @desc OUT
SELECT hResult = convert(varbinary(4), @hResult),
source = @source,
description = @desc,
FailPoint = 'SetRequestHeader failed',
MedthodName = @methodName
goto destroy
return
END
FETCH NEXT FROM RequestHeader
INTO @HeaderKey, @HeaderValue
END
CLOSE RequestHeader
DEALLOCATE RequestHeader
*/

-- send the request
EXEC @hResult = sp_OAMethod @objectID, 'send', null, @requestBody
IF @hResult <> 0
BEGIN
EXEC sp_OAGetErrorInfo @objectID, @source OUT, @desc OUT
SELECT hResult = convert(varbinary(4), @hResult),
source = @source,
description = @desc,
FailPoint = 'Send failed',
MedthodName = @methodName
goto destroy
return
END

declare @statusText varchar(1000), @status varchar(1000)
-- Get status text
exec sp_OAGetProperty @objectID, 'StatusText', @statusText out
exec sp_OAGetProperty @objectID, 'Status', @status out
select @status, @statusText, @methodName

-- Get response text
exec sp_OAGetProperty @objectID, 'responseText', @responseText out
IF @hResult <> 0
BEGIN
EXEC sp_OAGetErrorInfo @objectID, @source OUT, @desc OUT
SELECT hResult = convert(varbinary(4), @hResult),
source = @source,
description = @desc,
FailPoint = 'ResponseText failed',
MedthodName = @methodName
goto destroy
return
END
destroy:
exec sp_OADestroy @objectID
SET NOCOUNT OFF
go

declare @xmlOut varchar(8000)
exec spHTTPRequest 'YourUri', 'POST', 'xmlStuff', '', '', @xmlOut out
select @xmlOut

go
drop proc spHTTPRequest


Go with the flow & have fun! Else fight the flow
Go to Top of Page

Hommer
Aged Yak Warrior

808 Posts

Posted - 2006-03-17 : 10:25:38
Great stuff, just what I need. I will try it out.

Thank you!
Go to Top of Page

Hommer
Aged Yak Warrior

808 Posts

Posted - 2006-03-18 : 11:01:08
Hi, spirit,

I just got time to go through your script piece by piece. This is my first real close look at these sp_OAs.

However, I did not see where in your last call that you can supply a xml file. Maybe I need to check out things like OpenXML, for xml or SQLXML 3.0 to build a string as input variable to the spHTTPRequest?

Thanks!
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2006-03-18 : 12:03:31
put your xml into @requestBody. if it's too big change the parameter to ntext.

also change
EXEC @hResult = sp_OAMethod @objectID, 'send', null, @requestBody out
to
EXEC @hResult = sp_OAMethod @objectID, 'send', null, @requestBody

i'll also edit my post to change it.

Go with the flow & have fun! Else fight the flow
Go to Top of Page

Hommer
Aged Yak Warrior

808 Posts

Posted - 2006-03-20 : 17:29:34
Hi, Spirit,
I run your script, and did not see any error as you has coded.
Here is the response I got:
<OrderStatus xmlns="http://www.mavericklabel.com"> <Response> <Status>-100</Status> <Message>Poorly formed XML. Cannot find Request.</Message> </Response> </OrderStatus>

My xml is a copy of their testing data as:
<!DOCTYPE orderStatus SYSTEM "http://www.mavericklabel.com/xml/dtd/orderStatus_1.00.dtd">
<OrderStatus xmlns=”http://www.mavericklabel.com/”>
<Request>
<Header>
<VendorName>maverick</VendorName>
<VendorPassword>qwerty123</VendorPassword>
</Header>
<Package>
<OrderNumber>550</OrderNumber>
<TrackingNumber>Z1234567890</TrackingNumber>
</Package>
</Request>
</OrderStatus>

Their sample shows the header is embedded in the xml. Do I need to change any thing on your script that set the request header first then send the request?

Thanks!
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2006-03-21 : 05:19:13
there's a difference between request header and xml header.
if you want to add a request header check out
-- set request headers
section

well if you get a response xml back that means that the http post was succesfull.
so the problem lies within xml.

so i don't kn ow what else to tell you unless you can provide more info...

Go with the flow & have fun! Else fight the flow
Go to Top of Page

Hommer
Aged Yak Warrior

808 Posts

Posted - 2006-03-21 : 11:02:47
Sorry I did not get it right on my first try. I initiated the variables in the create proc spHTTPRequest!

Here is my new calling code and the reply I got: (I also tried ntext for @requestBody in case the xml stream is too long, but ntext is invalid for local variables). Thanks!

declare
@requestBody varchar(8000),
@UserName nvarchar(100),
@Password nvarchar(100),
@responseText varchar(8000)
select @requestBody = '<!DOCTYPE orderStatus SYSTEM "http://www.mavericklabel.com/xml/dtd/orderStatus_1.00.dtd"><OrderStatus xmlns=”http://www.mavericklabel.com/”><Header><VendorName>maverick</VendorName><VendorPassword>qwerty123</VendorPassword></Header><Package><OrderNumber>550</OrderNumber><TrackingNumber>Z1234567890</TrackingNumber></Package></OrderStatus>'
select @UserName ='myUname'
select @Password ='myPWord'
exec spHTTPRequest 'http://www.mavericklabel.com:2000/xml/orderStatus.php',
'POST', @requestBody, @UserName, @Password, @responseText


0x80070057 msxml3.dll The parameter is incorrect. Send failed POST
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2006-03-21 : 11:10:24
it should be:
exec spHTTPRequest 'http://www.mavericklabel.com:2000/xml/orderStatus.php',
'POST', @requestBody, @UserName, @Password, @responseText out

so which parameter was incorrect?



Go with the flow & have fun! Else fight the flow
Go to Top of Page

Hommer
Aged Yak Warrior

808 Posts

Posted - 2006-03-21 : 12:30:32
Thank you for your prompt post.
Even after I added keyword OUT to the last parameter, I still run into the same error message, and I have no clue which parameter(s) is wrong.
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2006-03-21 : 14:19:38
well there is:
IF @hResult <> 0
begin
...
end

after sp_OA* call so there's a select for each error.
so which select is shown?



Go with the flow & have fun! Else fight the flow
Go to Top of Page

Hommer
Aged Yak Warrior

808 Posts

Posted - 2006-03-21 : 16:13:10
This is all I got.

0x80070057 msxml3.dll The parameter is incorrect. Send failed POST

The FailPoint = 'Send failed', but like I said, I have no clue which parameter is incorrect.

Is there any thing I missed?
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2006-03-22 : 05:46:00
hm... you got me there...
have you tried a higher or a lower msxml version?

Go with the flow & have fun! Else fight the flow
Go to Top of Page

Hommer
Aged Yak Warrior

808 Posts

Posted - 2006-03-22 : 10:02:57

Ok, I will try different version of msxml. I got msxml6. I did not know your script is based on that library.
Also, I am curious how do you GET the response that belongs to you, because I did not see any common parameter between my POST and Get calls.

Here is my GET call from your example.

declare @xmlOut varchar(8000)
exec spHTTPRequest 'http://www.mavericklabel.com:2000/xml/orderStatus.php', 'GET', '', '', '', @xmlOut out
select @xmlOut
go
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2006-03-22 : 10:07:49
i'm not sure... what you mean by common parameters??
it's just a matter of specifying the url, method name and body to send.


Go with the flow & have fun! Else fight the flow
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2006-03-22 : 11:34:49
try adding
declare @len int
set @len = len(@requestBody)
EXEC @hResult = sp_OAMethod @objectID, 'setRequestHeader', null, 'Content-Length', @len
to the sproc.

i edited the original post with it.

Go with the flow & have fun! Else fight the flow
Go to Top of Page

Hommer
Aged Yak Warrior

808 Posts

Posted - 2006-03-22 : 16:26:24
Here are three different versions of msxml I tried and their results. The last one was my idea, and it seems the 2nd one got me closest to my target:

--EXEC @hResult = sp_OACreate 'Microsoft.XMLHTTP', @objectID OUT
--got the parameter is incorrect send failed

--EXEC @hResult = sp_OACreate 'MSXML2.ServerXMLHTTP.4.0', @objectID OUT
--got access is denied send failed

--EXEC @hResult = sp_OACreate 'MSXML.ServerXMLHTTP.2.0', @objectID OUT
--got create failed Invalid class string
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2006-03-22 : 16:44:47
have you tried adding the content length?

Go with the flow & have fun! Else fight the flow
Go to Top of Page

Hommer
Aged Yak Warrior

808 Posts

Posted - 2006-03-22 : 17:22:05
Yes, those results are after I added that block of code.
I thing I just found out on MS documentation is that MSMXL ServerXMLHTTP seems only work with ASP/IIS. In my case, that URL points to a PHP extension, and I don't know what kind of web server they are running.
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2006-03-22 : 17:32:02
hmmm.. interesting....
because i use this exact proc to call a web service with no problems.


Go with the flow & have fun! Else fight the flow
Go to Top of Page

fmunozag
Starting Member

1 Post

Posted - 2009-12-23 : 12:38:16
Hi all.
What I do if my XML size is more than 8000 chars?

Thankxx a lot.
Go to Top of Page
    Next Page

- Advertisement -