Problem: I am getting the following error output from If Err.Number <> 0 Then check;
Err.Number :-1072954818
Err.Source :msxml6.dll
Err.Source :This method cannot be called until the open method has been called.
Code:
dim objHttpRequest
dim gw_menu_request
dim HTTPMethod
HTTPMethod="POST"
Set objHttpRequest = Server.CreateObject("MSXML2.ServerXMLHTTP.6.0")
gw_menu_request = "http://test.com?q=headerexpose/expose_headers/expose_json"
objHttpRequest.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
objHttpRequest.setRequestHeader "Content-Length", 0
objHttpRequest.open HTTPMethod, gw_menu_request, false
Response.write(objHttpRequest.ResponseXML)
If Err.Number <> 0 Then
Response.Write "Err.Number :" & Err.Number & "<br/>"
Response.Write "Err.Source :" & Err.Source & "<br/>"
Response.Write "Err.Source :" & Err.Description & "<br/>"
Response.Write "Err.File :" & Err.File & "<br/>"
End If
What am I missing here?
The issue is exactly as the described in the error, you are trying to set Request Headers without first calling Open(). You are also missing the Send() method to send the request before a response can be received.
Dim objHttpRequest
Dim gw_menu_request
Dim HTTPMethod
HTTPMethod="POST"
Set objHttpRequest = Server.CreateObject("MSXML2.ServerXMLHTTP.6.0")
gw_menu_request = "http://test.com?q=headerexpose/expose_headers/expose_json"
'Open request specifying method and URL to call
objHttpRequest.open HTTPMethod, gw_menu_request, False
'Set any HTTP headers needed before sending.
objHttpRequest.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
objHttpRequest.setRequestHeader "Content-Length", 0
'Send the request
objHttpRequest.Send
Response.write(objHttpRequest.ResponseXML.Xml)
If Err.Number <> 0 Then
Response.Write "Err.Number :" & Err.Number & "<br/>"
Response.Write "Err.Source :" & Err.Source & "<br/>"
Response.Write "Err.Source :" & Err.Description & "<br/>"
Response.Write "Err.File :" & Err.File & "<br/>"
End If
You also want ResponseXML.Xml or you will receive a
Microsoft VBScript runtime error: Type mismatch
because you are trying to output the object not the Xml property that contains the XML string representation.
Related
I used the following code to check if a RSS url responds in 5 seconds so I can consume the RSS feed however trying to open the URL causes error when the target URL could not be resolved. What more I need rather than waitForResponse to also handle this situation?
Set http = Server.CreateObject("MSXML2.ServerXMLHTTP")
http.open "POST", "https://persiadigest.com/fa/rss/8", True
http.send
If http.waitForResponse(5) Then
body=http.responsetext
Else
response.write "Target url is not responding"
End If
Set http = Nothing
Error details:
msxml3.dll error '80072ee7' The server name or address could not be
resolved
Give a try for this example and tell me the results :
Option Explicit
Dim Title : Title = "Get RSS FEED"
Dim ArrURL : ArrURL = Array("https://persiadigest.com/fa/rss/8","http://khabarfoori.com/rss/mm")
Dim URL
For Each URL in ArrURL
If CheckURL(URL) = "200" Then
MsgBox chr(34) & URL & chr(34) & " ==> is active"& vbCrLF &_
"Status : " & CheckURL(URL),vbInformation,Title
MsgBox GetDataFromURL(URL,"GET",""),vbInformation,Title
Else
MsgBox chr(34) & URL & chr(34) & " ==> is inactive" & vbCrLF &_
"Status : " & CheckURL(URL),vbCritical,Title
End if
Next
'---------------------------------------------------------------------------
Function GetDataFromURL(strURL, strMethod, strPostData)
Dim lngTimeout
Dim strUserAgentString
Dim intSslErrorIgnoreFlags
Dim blnEnableRedirects
Dim blnEnableHttpsToHttpRedirects
Dim strHostOverride
Dim strLogin
Dim strPassword
Dim strResponseText
Dim objWinHttp
lngTimeout = 59000
strUserAgentString = "http_requester/0.1"
intSslErrorIgnoreFlags = 13056 ' 13056: ignore all err, 0: accept no err
blnEnableRedirects = True
blnEnableHttpsToHttpRedirects = True
strHostOverride = ""
strLogin = ""
strPassword = ""
Set objWinHttp = CreateObject("WinHttp.WinHttpRequest.5.1")
objWinHttp.SetTimeouts lngTimeout, lngTimeout, lngTimeout, lngTimeout
objWinHttp.Open strMethod, strURL
If strMethod = "POST" Then
objWinHttp.setRequestHeader "Content-type", _
"application/x-www-form-urlencoded"
End If
If strHostOverride <> "" Then
objWinHttp.SetRequestHeader "Host", strHostOverride
End If
objWinHttp.Option(0) = strUserAgentString
objWinHttp.Option(4) = intSslErrorIgnoreFlags
objWinHttp.Option(6) = blnEnableRedirects
objWinHttp.Option(12) = blnEnableHttpsToHttpRedirects
If (strLogin <> "") And (strPassword <> "") Then
objWinHttp.SetCredentials strLogin, strPassword, 0
End If
On Error Resume Next
objWinHttp.Send(strPostData)
If Err.Number = 0 Then
If objWinHttp.Status = "200" Then
GetDataFromURL = objWinHttp.ResponseText
Else
GetDataFromURL = "HTTP " & objWinHttp.Status & " " & _
objWinHttp.StatusText
End If
Else
GetDataFromURL = "Error " & Err.Number & " " & Err.Source & " " & _
Err.Description
End If
On Error GoTo 0
Set objWinHttp = Nothing
End Function
'---------------------------------------------------------------------------
Function CheckURL(vURL)
On Error Resume Next
Dim xhr
Set xhr = CreateObject("MSXML2.ServerXMLHTTP.3.0")
xhr.Open "HEAD", vURL, false
xhr.Send
If Err.Number = 0 Then
'MsgBox xhr.status
CheckURL = xhr.status
Else
CheckURL = Err.Description
End If
End Function
'---------------------------------------------------------------------------
A simple function that ignores errors would work.
Function WaitIgnoreError(ByRef requestObject, timeout)
On Error Resume Next
WaitIgnoreError = requestObject.WaitForResponse(timeout)
End Function
Usage:
If WaitIgnoreError(http, 5) Then
body = http.responsetext
Else
response.write "Target url is not responding"
End If
I want to send Image or pdf document in post request, the URL is working fine, but in visual basic I used the below code to send the document using url but It gives me failure response server error .
I have checked URL, it has no issue, but when I tried to implement in vb, in response I get the 500 server error
Dim strFile As String
Dim uploadDocUrl As String
Dim baBuffer() As Byte
Dim sPostData As String
Dim strFile As String
Dim strFileName As String
strFile = "C://Users/Avinashi/Desktop/1.pdf"
uploadDocUrl = "http://api.tally.messaging.bizbrain.in/api/v1/uploadFile"
strFileName = "1.pdf"
nFile = FreeFile
Open strFile For Binary Access Read As nFile
If LOF(nFile) > 0 Then
ReDim baBuffer(0 To LOF(nFile) - 1) As Byte
Get nFile, , baBuffer
sPostData = StrConv(baBuffer, vbUnicode)
End If
Close nFile
sPostData = "--" & STR_BOUNDARY & vbCrLf & _
"Content-Disposition: form-data;name=""1.pdf""; filename=""" & Mid$(strFile, InStrRev(strFile, "\") + 1) & """" & vbCrLf & _
"Content-Type:multipart/form-data" & vbCrLf & vbCrLf & _
sPostData & vbCrLf & _
"--" & STR_BOUNDARY & "--"
With CreateObject("Microsoft.XMLHTTP")
.Open "POST", uploadDocUrl, bAsync
.SetRequestHeader "Content-Type", "multipart/form-data"
.SetRequestHeader "token", "78bea912b4a5c497b85926bb471fab04"
.Send pvToByteArray(sPostData)
MsgBox (.responseText)
End With
Private Function pvToByteArray(sText As String) As Byte()
pvToByteArray = StrConv(sText, vbUnicode)
End Function
Why can't the code below connect successfully to servers that require SHA2 certs? One of our partners had a SHA2 upgrade and they require us to have SHA2 certs. When we connect to their site that doesn't require SHA2 certs(only SHA1) it connects successfully with the code below. When we try to connect to a site that requires SHA2 certs(secure.anywhere.org) we get the error: Server returned error - The data necessary to complete this operation is not yet available . All of the correct SHA2 certs are installed. We have the Root and Intermediate certs installed for SHA2. Could this be an issue with WinHTTP? Or is this possibly something with our environment configuration that we need to fix?
Full error:
Server returned error: -2147483638
The data necessary to complete this operation is not yet available.
Code used to connect:
<%
On Error Resume Next
Response.ContentType = "text/html"
Dim xobj
Dim sPostData
Set xobj = CreateObject("WinHttp.WinHttpRequest.5.1")
If Err.Number <> 0 Then
Response.Write "<b>"
Response.Write "Error Creating XMLHTTP: " & Err.Number & "<BR>" & Err.Description
Response.Write "</b>"
Response.End
End If
If Not IsObject(xobj) Then
Response.Write "<b>"
Response.Write "xobj is not an object: " & Err.Number & "<BR>" & Err.Description
Response.Write "</b>"
Response.End
End If
qu = Request.form("SSN")
sPostData
sPostData = "user_id=abcd1234&password=67judlaufosaf&qu=" & qu
xobj.Open "POST", "https://www.anywhere.org/secure_area/test_panel.asp", False
xobj.setRequestHeader "Referer", "https://www2.homeu.nc.edu/tns/connect.asp"
xobj.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
xobj.Send sPostData
response.buffer = True
response.clear
response.ContentType = xobj.getResponseHeader("Content-Type")
response.AddHeader "Content-Type", xobj.getResponseHeader("Content-Type")
response.BinaryWrite xobj.responseBody
If Err.Number <> 0 Then
Response.Write "<b>"
Response.Write "Server returned error: " & Err.Number & "<BR>" & Err.Description
Response.Write "</b>"
Set xobj = nothing
Response.End
End If
Set xobj = nothing
response.end
%>
This issue was due to Windows not having enough information to verify the SHA-2 cert. When we checked the status of the certs it had a status of "The issuer of this certificate could not be found". Here is a link to the solution.
I am attempting to merge parts of several wiki pages to create a condensed/summarized version.
The following code works for pages like google.com.
But, it's returning a blank result for the page I want.
Any suggestions?
Set o = CreateObject("MSXML2.ServerXMLHTTP.6.0")
o.open "GET", "http://stevescarts2.wikispaces.com/Modules", False
o.send
If err.number = 0 then
MsgBox o.responseText
Else
MsgBox "error " & err.number & ": " & err.description
End If
There is no response text. The server responds with status 302 Found to that request. Always check the status when dealing with XMLHTTP requests:
WScript.Echo o.status & " " & o.statusText
Thanks, Ansgar.
Much appreciated.
Here's the modified code that works for me...
Set o = CreateObject("MSXML2.ServerXMLHTTP.6.0")
url = "http://stevescarts2.wikispaces.com/Modules"
Do
o.open "GET", url, False
o.send
If o.Status = 302 Then
url = o.GetResponseHeader("Location")
End If
Loop Until err.number = 0 And o.Status <> 302
If err.number = 0 then
MsgBox o.responseText
ElseIf o.status & " " & o.statusText Then
MsgBox "error " & err.number & ": " & err.description
End If
Here is the code I am using:
Dim httpReq As New WinHttp.WinHttpRequest
Dim strLineOut As String
Dim strReturn As String
Dim strStatus As String
lblResponse1.Caption = ""
DoEvents
strLineOut = "<form name=""form1"" method=""post"" enctype=""multipart/form-data"">" & vbCrLf
strLineOut = strLineOut & " <input name=""hdntype"" type=""hidden"" id=""hnd1"" value=""1"">" & vbCrLf
strLineOut = strLineOut & " <input name=""hnd1"" type=""hidden"" id=""hnd1"" value=""Value1"">" & vbCrLf
strLineOut = strLineOut & " <input name=""hdn2"" type=""hidden"" id=""hdn2"" value=""Value2"">" & vbCrLf
strLineOut = strLineOut & " <input type=""submit"" name=""Submit"" value=""Submit"">" & vbCrLf
strLineOut = strLineOut & "</form>" & vbCrLf
httpReq.Open "POST", "http://www.XXXX.com/XMLProjects/vb6test/form_post.asp", False
httpReq.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
'text/xml
'application/x-www-form-urlencoded
'httpReq.StatusText
'httpReq.Status
'httpReq.SetRequestHeader "Content-Length", Len(strLineOut)
httpReq.Send (strLineOut)
strStatus = httpReq.StatusText
strReturn = httpReq.ResponseText
Debug.Print strReturn & vbCrLf & strStatus
lblResponse1.Caption = strReturn & vbCrLf & strStatus
Set httpReq = Nothing
The asp that catches the form cannot seem to recognize the form. It sees a form with one item.
The catch code in the asp is:
Response.Write Request.Form("hdntype")
Response.Write "the form object is " & Request.Form.Item(1) & vbCrLf
The response from the asp is:
the form object is "form1"method="post"enctype="multipart/form-data">
<inputname="hdntype"type="hidden"id="hnd1"value="1">
<inputname="hnd1"type="hidden"id="hnd1"value="Nick">
<inputname="hdn2"type="hidden"id="hdn2"value="Arnone">
<inputtype="submit"name="Submit"value="Submit"></form>
It does not see the item hdntype, or any other item within the form. It sees 1 item, the entire form.
If I do a Request.TotalBytes, I can see everythinhg in the asp.
If I add a querystring objects, I can see each object.
I cannot see form objects.
In VB6, if you send the data like this:
strIDJob = "34"
strAuthString = "supertest"
DataToPost = ""
DataToPost = DataToPost & "IDJob=" & strIDJob & "&"
DataToPost = DataToPost & "AUTH=" & strAuthString & "&"
(im sending it to an ASP page, using the CreateObject("Msxml2.XMLHTTP.6.0") component)
(sending with POST with only this header included: "application/x-www-form-urlencoded")
Then, you can retrieve each item using the code bellow (in ASP), one by one:
IDJob = Request.Form.Item(1) 'here is the core point of this post. This is the line that matters
AUTH = Request.Form.Item(2) 'here is the core point of this post. This is the line that matters
response.write "IDJob = " & IDJob & "<BR>"
response.write "AUTH = " & AUTH & "<BR>"
Response.End
this code in asp produces the following return/output:
IDJob = 34AUTH = supertest