Advertisement
Guest User

Untitled

a guest
May 24th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.91 KB | None | 0 0
  1. CREATE PROCEDURE [dbo].[spHTTPRequest]
  2. @URI varchar(2000) = '',
  3. @methodName varchar(50) = '',
  4. @requestBody varchar(8000) = '',
  5. @SoapAction varchar(255),
  6. @UserName nvarchar(100), -- DomainUserName or UserName
  7. @Password nvarchar(100),
  8. @responseText varchar(8000) output
  9. AS
  10. SET NOCOUNT ON
  11.  
  12. IF @methodName = ''
  13. BEGIN
  14. SELECT FailPoint = 'Method Name must be set'
  15. RETURN
  16. END
  17.  
  18. SET @responseText = 'FAILED'
  19.  
  20. DECLARE @objectID int
  21. DECLARE @hResult int
  22. DECLARE @source varchar(255), @desc varchar(255)
  23.  
  24. EXEC @hResult = sp_OACreate 'MSXML2.ServerXMLHTTP', @objectID OUT
  25.  
  26. IF @hResult <> 0
  27. BEGIN
  28. EXEC sp_OAGetErrorInfo @objectID, @source OUT, @desc OUT
  29.  
  30. SELECT
  31. hResult = convert(varbinary(4), @hResult),
  32. source = @source,
  33. description = @desc,
  34. FailPoint = 'Create failed',
  35. MethodName = @methodName
  36.  
  37. GOTO destroy
  38. RETURN
  39. END
  40.  
  41. -- open the destination URI with Specified method
  42. EXEC @hResult = sp_OAMethod @objectID, 'open', null, @methodName, @URI, 'false', @UserName, @Password
  43.  
  44. IF @hResult <> 0
  45. BEGIN
  46. EXEC sp_OAGetErrorInfo @objectID, @source OUT, @desc OUT
  47.  
  48. SELECT
  49. hResult = convert(varbinary(4), @hResult),
  50. source = @source,
  51. description = @desc,
  52. FailPoint = 'Open failed',
  53. MethodName = @methodName
  54.  
  55. GOTO destroy
  56. RETURN
  57. END
  58.  
  59. -- set request headers
  60. EXEC @hResult = sp_OAMethod @objectID, 'setRequestHeader', null, 'Content-Type', 'text/xml;charset=UTF-8'
  61.  
  62. IF @hResult <> 0
  63. BEGIN
  64. EXEC sp_OAGetErrorInfo @objectID, @source OUT, @desc OUT
  65.  
  66. SELECT
  67. hResult = convert(varbinary(4), @hResult),
  68. source = @source,
  69. description = @desc,
  70. FailPoint = 'SetRequestHeader failed',
  71. MethodName = @methodName
  72.  
  73. GOTO destroy
  74. RETURN
  75. END
  76.  
  77. -- set soap action
  78. EXEC @hResult = sp_OAMethod @objectID, 'setRequestHeader', null, 'SOAPAction', @SoapAction
  79.  
  80. IF @hResult <> 0
  81. BEGIN
  82. EXEC sp_OAGetErrorInfo @objectID, @source OUT, @desc OUT
  83.  
  84. SELECT
  85. hResult = convert(varbinary(4), @hResult),
  86. source = @source,
  87. description = @desc,
  88. FailPoint = 'SetRequestHeader failed',
  89. MethodName = @methodName
  90.  
  91. GOTO destroy
  92. RETURN
  93. END
  94.  
  95. declare @len int
  96. set @len = len(@requestBody)
  97.  
  98. EXEC @hResult = sp_OAMethod @objectID, 'setRequestHeader', null, 'Content-Length', @len
  99. IF @hResult <> 0
  100. BEGIN
  101. EXEC sp_OAGetErrorInfo @objectID, @source OUT, @desc OUT
  102. SELECT hResult = convert(varbinary(4), @hResult),
  103. source = @source,
  104. description = @desc,
  105. FailPoint = 'SetRequestHeader failed',
  106. MedthodName = @methodName
  107. goto destroy
  108. return
  109. END
  110. /*
  111. -- if you have headers in a table called RequestHeader you can go through them with this
  112. DECLARE @HeaderKey varchar(500), @HeaderValue varchar(500)
  113. DECLARE RequestHeader CURSOR
  114. LOCAL FAST_FORWARD
  115. FOR
  116. SELECT HeaderKey, HeaderValue
  117. FROM RequestHeaders
  118. WHERE Method = @methodName
  119. OPEN RequestHeader
  120. FETCH NEXT FROM RequestHeader
  121. INTO @HeaderKey, @HeaderValue
  122. WHILE @@FETCH_STATUS = 0
  123. BEGIN
  124. --select @HeaderKey, @HeaderValue, @methodName
  125. EXEC @hResult = sp_OAMethod @objectID, 'setRequestHeader', null, @HeaderKey, @HeaderValue
  126. IF @hResult <> 0
  127. BEGIN
  128. EXEC sp_OAGetErrorInfo @objectID, @source OUT, @desc OUT
  129. SELECT hResult = convert(varbinary(4), @hResult),
  130. source = @source,
  131. description = @desc,
  132. FailPoint = 'SetRequestHeader failed',
  133. MedthodName = @methodName
  134. goto destroy
  135. return
  136. END
  137. FETCH NEXT FROM RequestHeader
  138. INTO @HeaderKey, @HeaderValue
  139. END
  140. CLOSE RequestHeader
  141. DEALLOCATE RequestHeader
  142. */
  143. -- send the request
  144. EXEC @hResult = sp_OAMethod @objectID, 'send', null, @requestBody
  145. IF @hResult <> 0
  146. BEGIN
  147. EXEC sp_OAGetErrorInfo @objectID, @source OUT, @desc OUT
  148. SELECT hResult = convert(varbinary(4), @hResult),
  149. source = @source,
  150. description = @desc,
  151. FailPoint = 'Send failed',
  152. MedthodName = @methodName
  153. goto destroy
  154. return
  155. END
  156. declare @statusText varchar(1000), @status varchar(1000)
  157. -- Get status text
  158. exec sp_OAGetProperty @objectID, 'StatusText', @statusText out
  159. exec sp_OAGetProperty @objectID, 'Status', @status out
  160. select @status, @statusText, @methodName
  161. -- Get response text
  162. exec sp_OAGetProperty @objectID, 'responseText', @responseText out
  163. IF @hResult <> 0
  164. BEGIN
  165. EXEC sp_OAGetErrorInfo @objectID, @source OUT, @desc OUT
  166. SELECT hResult = convert(varbinary(4), @hResult),
  167. source = @source,
  168. description = @desc,
  169. FailPoint = 'ResponseText failed',
  170. MedthodName = @methodName
  171. goto destroy
  172. return
  173. END
  174. destroy:
  175. exec sp_OADestroy @objectID
  176. SET NOCOUNT OFF
  177.  
  178. GO
  179.  
  180. declare @xmlOut varchar(8000)
  181. Declare @RequestText as varchar(8000);
  182. set @RequestText=
  183. '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
  184. <soapenv:Header/>
  185. <soapenv:Body>
  186. <tem:CreateOrder>
  187. <!--Optional:-->
  188. <tem:OrderRequest>
  189. <tem:OrderId>200</tem:OrderId>
  190. <!--Optional:-->
  191. <tem:OrderName>something</tem:OrderName>
  192. </tem:OrderRequest>
  193. </tem:CreateOrder>
  194. </soapenv:Body>
  195. </soapenv:Envelope>'
  196. exec spHTTPRequest
  197. 'http://localhost:8080/WebSite1/Service.asmx/HelloWorld',
  198. 'post',
  199. @RequestText,
  200. 'http://tempuri.org/CreateOrderForMe',
  201. '', '', @xmlOut out
  202. select @xmlOut
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement