i'm trying write macro querying web update access database. reason, vba refuses play friendly http, totally content https.
here's requesting function:
function httprequest(byval url string, useproxy boolean) string dim response string dim proxy string dim xhr object 'make http requester object set xhr = createobject("msxml2.serverxmlhttp.6.0") if useproxy if left(url, 5) = "https" proxy = "proxyb1secure:8443" else proxy = "proxyb1:8080" end if xhr.setproxy 2, proxy end if xhr.open "get", url, false 'send request. line times out on http xhr.send 'fetch whole page response = xhr.responsetext 'clean set xhr = nothing 'return httprequest = response end function and testing function:
function testproxy() 'this 1 works msgbox (httprequest("https://www.bing.com/search?q=stackoverflow", true)) 'this 1 doesn't. msgbox (httprequest("http://www.bing.com/search?q=stackoverflow", true)) end function i'm i'm going after right name , port, because i've tested same thing via java, , it's content both flavors (i.e. works in following code).
public static void main(string[] args) throws exception { url url = new url("http://www.bing.com/search?q=stackoverflow"); httpurlconnection con = (httpurlconnection) url.openconnection(getproxyforurl(url)); system.out.println(con.getresponsecode() + " " + con.getresponsemessage()); inputstream = con.getinputstream(); int c; stringbuilder sb = new stringbuilder(); while ((c = is.read()) != -1) { sb.append((char) c); } string page = sb.tostring(); system.out.println(page); } public static proxy getproxyforurl(url url) { if (url.getprotocol().equals("https")) { return new proxy(proxy.type.http, new inetsocketaddress("proxyb1secure", 8443)); } else { return new proxy(proxy.type.http, new inetsocketaddress("proxyb1", 8080)); } } what trickery of vba missing?
mystery solved. turns out security feature revolving around user agent (of things...).
java used these http headers (which successful):
get http://www.bing.com/search?q=stackoverflow http/1.1 accept: */* accept-language: en-us proxy-connection: keep-alive user-agent: java/1.7.0_79 host: www.bing.com and access sent these (which unsuccessful):
get http://www.bing.com/search?q=stackoverflow http/1.1 accept: */* accept-language: en-us user-agent: mozilla/4.0 (compatible; win32; winhttp.winhttprequest.5) proxy-connection: keep-alive host: www.bing.com by adding
xhr.setrequestheader "user-agent", "paymenoattention" it magically goes through. , confirm theory, adding access' user-agent java caused fail. so. that's what's up.
this attempt our brilliant network techs block macro viruses contacting malicious sites.
Comments
Post a Comment