0

I am using Winhttp in the following VBA code and can't get the URL to work, yet if I paste the URL into a browser it works fine. Any ideas would be appreciated:

Sub WinHttp()

Dim arr() As String
Dim pos As Integer
Dim used As Range

Dim url, resp As String
Dim req As New WinHttpRequest
Dim n As Integer

r = ActiveSheet.UsedRange.Rows.Count

For n = 4 To r

    Worksheets("Sheet0").Activate
    pn = Cells(n, 2).Value
    UnitPrice = (Cells(n, 4).Value) * 1

    url = "https://www.google.com/search?q=DNR-12-1G+$4,250"

    req.Open "GET", url, False
    req.send

    resp = req.ResponseText

    Debug.Print resp

    arr = split(resp)

    arr = Filter(arr, "$")

Next n

End Sub
Teamothy
  • 2,000
  • 3
  • 16
  • 26
Bob
  • 101
  • 3
  • 13
  • Possible duplicate of [HTTPS POST request using VBA for Excel](http://stackoverflow.com/q/1264303/11683) – GSerg Dec 25 '16 at 20:53
  • When I put it in a web debugger goggle returned error 302 and a nice web page but as it's an error XMLHTTP doesn't show it. But the 302 says it has moved and there is a redirect. I also escaped your link after `q=` with VBScript Escape function which gives this `DNR-12-1G+%244%2C250` –  Dec 26 '16 at 01:03
  • 302 Moved 302 Moved The document has moved here. –  Dec 26 '16 at 01:04
  • If you change it to your local goggle. It works if I put in `goggle.com.au`. –  Dec 26 '16 at 01:15

2 Answers2

0

Just tested the following code in Excel 2013, where it runs fine:

Sub Test()
    Dim req As New WinHttpRequest
    Dim url, resp As String

    url = "https://www.google.com/search?q=DNR-12-1G+$4,250"

    req.Open "GET", url, False
    req.send

    resp = req.ResponseText
    Debug.Print resp
End Sub

The response from the server is

<!DOCTYPE html><html lang=en><meta charset=utf-8><meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width"><title>Error 403 (Forbidden)!!1</title><style>*{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/branding/googlelogo/1x/googlelogo_color_150x54dp.png) no-repeat;margin-left:-5px}@media only screen and (min-resolution:192dpi){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat 0% 0%/100% 100%;-moz-border-image:url(//www.google.com/images/branding/googlelogo/2x/
googlelogo_color_150x54dp.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:54px;width:150px}</style><a href=//www.google.com/><span id=logo aria-label=Google></span></a><p><b>403.</b> <ins>That’s an error.</ins><p>Your client does not have permission to get URL <code>/search?q=DNR-12-1G+%244,250&amp;gws_rd=cr&amp;ei=IDVgWJ2dDomnsgGfy4u4Dw</code> from this server.  (Client IP address: 194.28.248.113)<br><br>
Please see Google's Terms of Service posted at http://www.google.com/terms_of_service.html
<BR><BR><P>If you believe that you have received this response in error, please <A HREF="https://www.google.com/support/contact/user?hl=en">report</A> your problem. However, please make sure to take a look at our Terms of Service (http://www.google.com/terms_of_service.html). In your email, please send us the <b>entire</b> code displayed below.  Please also send us any information you may know about how you are performing your Google searches-- for example, "I'm using the Opera browser on Linux to do searches from home.  My Internet access is through a dial-up account I have with the FooCorp ISP." or "I'm using the Konqueror browser on Linux to search from my job at myFoo.com.  My machine's IP address is 10.20.30.40, but all of myFoo's web traffic goes through some kind of proxy server whose IP address is 10.11.12.13."  (If you don't know any information like this, that's OK.  But this kind of information can help us track down problems, so please tell us what you can.)</P><P>We will use all this information
 to diagnose the problem, and we'll hopefully have you back up and searching with Google again quickly!</P>
<P>Please note that although we read all the email we receive, we are not always able to send a personal response to each and every email.  So don't despair if you don't hear back from us!</P>
<P>Also note that if you do not send us the <b>entire</b> code below, <i>we will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TE>


  <ins>That’s all we know.</ins>
fuglede
  • 17,388
  • 2
  • 54
  • 99
  • That is a customized [HTTP 403](https://en.wikipedia.org/wiki/HTTP_403), which I believe the OP counts as "can't get the URL to work". – GSerg Dec 25 '16 at 21:14
  • Could be. In that case, that has very little to do with VBA/WinHTTP though. – fuglede Dec 25 '16 at 21:16
  • Could be, but [not necessarily](http://stackoverflow.com/q/1264303/11683). – GSerg Dec 25 '16 at 21:17
  • Well, you are generally not supposed to use Google for scripting, so that's what's causing the 403 in this case. You can get around it with `req.SetRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"`, but you will want to check up on the terms of service first. – fuglede Dec 25 '16 at 21:24
  • Fuglede: Look closely at the error in the return: "client does not have permission to get URL /search?q=DNR-12-1G+%244,250&gws_rd=cr&ei=IDVgWJ2dDomnsgGfy4u4Dw from this server." – Bob Dec 25 '16 at 23:41
  • 1
    @Bob: That's Google sending you that error: The request is going through just fine. If you want to cheat their User-Agent blocker, you can use the header in the comment above, but check up on their terms of service first. – fuglede Dec 25 '16 at 23:46
0

Thanks! It appears the "req.SetRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"

Does the trick.

Bob
  • 101
  • 3
  • 13