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.
| Author |
Topic |
|
esthera
Master Smack Fu Yak Hacker
1410 Posts |
Posted - 2006-06-09 : 07:58:52
|
I have the following code below.It takes a url and posts and then retrieves the response.It works fine except the response is not returned. Any ideas on what I need to fix to return the response.CREATE procedure HTTP_POST( @sUrl varchar(2000), @response varchar(8000) output, @status int output )AsDeclare @obj intdeclare @hr intexec @hr = sp_OACreate 'MSXML2.ServerXMLHttp', @obj OUT-- exec @hr = sp_OACreate 'MSXML2.ServerXMLHttp', @obj OUTif @hr <> 0 begin set @status = -1 set @response = 'sp_OACreate MSXML2.ServerXMLHttp.3.0 failed' return endexec @hr = sp_OAMethod @obj, 'open', NULL, 'POST', @sUrl, falseif @hr <>0 begin set @status = -2 set @response = 'sp_OAMethod Open failed' exec @hr = sp_OADestroy @obj returnendexec @hr = sp_OAMethod @obj, 'setRequestHeader', NULL, 'Content-Type','application/x-www-form-urlencoded'if @hr <>0 begin set @status = -3 set @response = 'sp_OAMethod setRequestHeader failed' exec @hr = sp_OADestroy @obj returnendexec @hr = sp_OAMethod @obj, send, NULL, ''if @hr <>0 begin set @status = -4 set @response = 'sp_OAMethod Send failed' exec @hr = sp_OADestroy @obj returnendexec @hr = sp_OAGetProperty @obj, 'status', @status OUTif @hr <>0 begin set @status = -5 set @response = 'sp_OAMethod read status failed' exec @hr = sp_OADestroy @obj returnendif @status <> 200 begin set @response = 'sp_OAMethod http status ' + cast(@status as varchar(20) ) exec @hr = sp_OADestroy @obj returnendexec @hr = sp_OAGetProperty @obj, 'responseText', @response OUTif @hr <>0 begin set @status = -6 set @response = 'sp_OAMethod read response failed' exec @hr = sp_OADestroy @obj returnendexec @hr = sp_OADestroy @objreturnGO |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2006-06-09 : 08:24:03
|
try setting the content-length header also.Go with the flow & have fun! Else fight the flow Blog thingie: [URL="http://weblogs.sqlteam.com/mladenp"] |
 |
|
|
esthera
Master Smack Fu Yak Hacker
1410 Posts |
Posted - 2006-06-09 : 09:16:14
|
| can you explain to me what you mean? |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2006-06-09 : 09:19:53
|
declare @len intset @len = len(@sUrl)exec @hr = sp_OAMethod @obj, 'setRequestHeader', NULL, 'Content-Length', @lenif @hr <>0 begin set @status = -3 set @response = 'sp_OAMethod setRequestHeader failed' exec @hr = sp_OADestroy @obj returnendGo with the flow & have fun! Else fight the flow Blog thingie: [URL="http://weblogs.sqlteam.com/mladenp"] |
 |
|
|
esthera
Master Smack Fu Yak Hacker
1410 Posts |
Posted - 2006-06-11 : 01:51:48
|
| if i do this i get an error sp_OAMethod setRequestHeader failed |
 |
|
|
esthera
Master Smack Fu Yak Hacker
1410 Posts |
Posted - 2006-06-12 : 02:07:40
|
| does anyone have any idea what i can do to debug this? |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2006-06-12 : 05:43:32
|
to what page are you trying to post this?is it php or asp?there was a discussion here about this not working for posts to php pages a while ago.when i tried to do the same thing i randomly sometimes got the respose and sometimes not.so i wouldn't count this as very reliable method.what are you trying to do? maybe it could be done in a different way.Go with the flow & have fun! Else fight the flow Blog thingie: [URL="http://weblogs.sqlteam.com/mladenp"] |
 |
|
|
afrika
Master Smack Fu Yak Hacker
2706 Posts |
Posted - 2006-06-12 : 07:50:50
|
| This is interesting, i never knew u could execute the XMLHTTP object via a stored proceedure, we use this regularly in ASP pages. but this is awesome... |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2006-06-12 : 08:28:15
|
yes it can be done, but i find it unreliable.this the code i usecreate proc spHTTPRequest @URI varchar(2000) = '', @methodName varchar(50) = '', @requestBody varchar(8000) = '', @responseText varchar(8000) outputasSET NOCOUNT ONIF @methodName = ''BEGIN select FailPoint = 'Method Name must be set' returnEND set @responseText = 'FAILED'DECLARE @objectID intDECLARE @hResult intDECLARE @source varchar(255), @desc varchar(255)EXEC @hResult = sp_OACreate 'MSXML2.ServerXMLHTTP.4.0', @objectID OUT--EXEC @hResult = sp_OACreate 'Microsoft.XMLHTTP', @objectID OUTIF @hResult <> 0BEGIN 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 returnEND-- open the destination URI with Specified methodEXEC @hResult = sp_OAMethod @objectID, 'open', null, @methodName, @URI, 'false'IF @hResult <> 0BEGIN 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 returnEND-- set request headersEXEC @hResult = sp_OAMethod @objectID, 'setRequestHeader', null, 'Content-Type', 'text/xml'IF @hResult <> 0BEGIN EXEC sp_OAGetErrorInfo @objectID, @source OUT, @desc OUT SELECT hResult = convert(varbinary(4), @hResult), source = @source, description = @desc, FailPoint = 'SetRequestHeader failed: Content-Type', MedthodName = @methodName goto destroy returnENDdeclare @len intset @len = len(@requestBody)EXEC @hResult = sp_OAMethod @objectID, 'setRequestHeader', null, 'Content-Length', @lenIF @hResult <> 0BEGIN EXEC sp_OAGetErrorInfo @objectID, @source OUT, @desc OUT SELECT hResult = convert(varbinary(4), @hResult), source = @source, description = @desc, FailPoint = 'SetRequestHeader failed: Content-Length', MedthodName = @methodName goto destroy returnEND-- send the requestselect @requestBodyEXEC @hResult = sp_OAMethod @objectID, 'send', null, @requestBodyIF @hResult <> 0BEGIN 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 returnENDdeclare @statusText varchar(1000), @status varchar(1000)-- Get status textexec sp_OAGetProperty @objectID, 'StatusText', @statusText out exec sp_OAGetProperty @objectID, 'Status', @status out select @status, @statusText, @methodName-- Get response textexec sp_OAGetProperty @objectID, 'responseText', @responseText out IF @hResult <> 0BEGIN 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 returnENDdestroy: exec sp_OADestroy @objectIDSET NOCOUNT OFFgo Go with the flow & have fun! Else fight the flow Blog thingie: [URL="http://weblogs.sqlteam.com/mladenp"] |
 |
|
|
afrika
Master Smack Fu Yak Hacker
2706 Posts |
Posted - 2006-06-12 : 08:37:10
|
| Why do you say unreliable.We use it a lot, for web 2phone sms and domain name search....Also if other server side scripts, search, or posts need to be done, we use it.It has proven to be extremely reliable for ASP pages for us. But with Stored Proceedures, this is new... |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2006-06-12 : 08:42:10
|
i mean it's use from sql server is unreliable.at least in my experience.from asp it's ok. we use that too. Go with the flow & have fun! Else fight the flow Blog thingie: [URL="http://weblogs.sqlteam.com/mladenp"] |
 |
|
|
afrika
Master Smack Fu Yak Hacker
2706 Posts |
Posted - 2006-06-12 : 11:13:21
|
Guess its one of those SQL features that would be classified with cursors |
 |
|
|
|
|
|
|
|