AutoHotkey Community

It is currently May 27th, 2012, 1:17 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 233 posts ]  Go to page Previous  1 ... 12, 13, 14, 15, 16

Should a more generic function be released which will include the already available web functions, such as header queries, uri encoding, base encoding, etc ?
yes, i'd like to have one function to get all neccessary http functionalities in one instead of collecting each for my own
no, i prefer collecting the functions i need
neither nor ... explained in post
You may select 1 option

View results
Author Message
 Post subject:
PostPosted: August 8th, 2011, 8:26 pm 
Offline

Joined: November 22nd, 2006, 10:11 pm
Posts: 20
I'm not able to get post params working with Uberi's version from Feb 11th. It seems to be only sending the first character of the post body?

I'm pretty lost trying to figure out the problem, anyone have any idea?

Autohotkey_L 1.1.02.00 Unicode 32 Bit
Tested on Windows XP SP 2 and Win7 Home Premium


Example:
Code:
MsgBox % HTTPQuery(Result,"http://posttestserver.com/post.php?dump","test=123") . "`n""" . Result . """"

HTTPQuery(ByRef Result,URL,POSTData = "",Headers = "",POSTDataLength = "")
{
 ;Currently the verbs showHeader, storeHeader, and updateSize are supported in httpQueryOps. In case you need a different UserAgent, Proxy, ProxyBypass, Referrer, and AcceptType just specify them as global variables. Also if any special Flags are needed such as INTERNET_FLAG_NO_AUTO_REDIRECT or cache handling this might be set using the httpQueryDwFlags variable as global
 global httpQueryOps, httpAgent, httpProxy, httpProxyBypass, httpQueryReferrer, httpQueryAcceptType, httpQueryDwFlags, HttpQueryFullSize, HttpQueryHeader, HttpQueryCurrentSize
 CharSize := A_IsUnicode ? 2 : 1, Suffix := A_IsUnicode ? "W" : "A", UPtr := A_PtrSize ? "UPtr" : "UInt", Char := A_IsUnicode ? "UShort" : "UChar", PtrSize := A_PtrSize ? A_PtrSize : 4, StrGetFunc := "StrGet"

 If httpAgent =
  httpAgent = AutoHotkeyScript
 If httpProxy =
  httpProxy = 0
 If httpProxyBypass =
  httpProxyBypass = 0

 VarSetCapacity(URL_COMPONENTS,(6 * PtrSize) + 36,0), Offset := 0
 NumPut(60,URL_COMPONENTS,Offset,"UInt"), Offset += 4
 VarSetCapacity(lpszScheme,255,0), NumPut(&lpszScheme,URL_COMPONENTS,Offset), Offset += PtrSize, NumPut(255,URL_COMPONENTS,Offset,"UInt"), Offset += 4
 NumPut(255,URL_COMPONENTS,Offset,"UInt"), Offset += 4
 VarSetCapacity(lpszHostName,1024,0), NumPut(&lpszHostName,URL_COMPONENTS,Offset), Offset += PtrSize, NumPut(1024,URL_COMPONENTS,Offset,"UInt"), Offset += 4
 NumPut(0xFFFF,URL_COMPONENTS,Offset,"UInt"), Offset += 4
 VarSetCapacity(lpszUserName,1024,0), NumPut(&lpszUserName,URL_COMPONENTS,Offset), Offset += PtrSize, NumPut(1024,URL_COMPONENTS,Offset,"UInt"), Offset += 4
 VarSetCapacity(lpszPassword,1024,0), NumPut(&lpszPassword,URL_COMPONENTS,Offset), Offset += PtrSize, NumPut(1024,URL_COMPONENTS,Offset,"UInt"), Offset += 4
 VarSetCapacity(lpszURLPath,1024,0), NumPut(&lpszURLPath,URL_COMPONENTS,Offset), Offset += PtrSize, NumPut(1024,URL_COMPONENTS,Offset,"UInt"), Offset += 4
 VarSetCapacity(lpszExtraInfo,1024,0), NumPut(&lpszExtraInfo,URL_COMPONENTS,Offset), Offset += PtrSize, NumPut(1024,URL_COMPONENTS,Offset), Offset += 4
 hModule := DllCall("LoadLibrary","Str","WinINet.dll")
 ;Split the given URL: extract scheme, user, pass, authority (host), port, path, and query (extrainfo)
 If Not DllCall("WinINet\InternetCrackUrl" . Suffix,UPtr,&URL,"UInt",StrLen(URL),"UInt",0,UPtr,&URL_COMPONENTS)
  Return, "Error retrieving URL components."
 ;Update variables to length
 VarSetCapacity(lpszScheme,-1), VarSetCapacity(lpszHostName,-1), VarSetCapacity(lpszUserName,-1), VarSetCapacity(lpszPassword,-1), VarSetCapacity(lpszURLPath,-1), VarSetCapacity(lpszExtraInfo,-1)
 nPort := NumGet(URL_COMPONENTS,24)

 lpszUrlPath .= lpszExtrainfo

 ;detect additional Flags
 Flags := httpQueryDwFlags

 ;SECURITY_FLAG_IGNORE_UNKNOWN_CA=0x00000100
 ;SECURITY_FLAG_IGNORE_CERT_DATE_INVALID=0x00002000
 If lpszScheme = https
  Flags |= 0x801200 ;INTERNET_FLAG_SECURE | SECURITY_FLAG_IGNORE_CERT_CN_INVALID | SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE

 ;detect unknown or invalid URL
 If lpszScheme = unknown
  Return, "Invalid URL."
 hInternet := DllCall("WinINet\InternetOpen" . Suffix,UPtr,&httpAgent,"UInt",(httpProxy ? 3 : 1),UPtr,&httpProxy,UPtr,&httpProxyBypass,"UInt",0)
 hConnect := DllCall("WinINet\InternetConnect" . Suffix,UPtr,hInternet,UPtr,&lpszHostname,"Int",nPort,UPtr,lpszUsername ? &lpszUsername : 0,UPtr,lpszPassword ? &lpszPassword : 0,"UInt",3,"UInt",0,"UInt*",0)
 If Not hConnect
  Return, "Connection failed."

 ;Select the correct HTTP verb
 If POSTData =
  HTTPVerb = GET
 Else
 {
  HTTPVerb = POST
  If Headers =
   Headers = Content-Type: application/x-www-form-urlencoded
 }

 hRequest := DllCall("WinINet\HttpOpenRequest" . Suffix,UPtr,hConnect,UPtr,&HTTPVerb,UPtr,&lpszUrlPath,"Str","HTTP/1.1",UPtr,&httpQueryReferrer,UPtr,&httpQueryAcceptTypes,"UInt",Flags,"UInt",0)
 If Not hRequest
  Return, "Error opening request."

 If Not DllCall("WinINet\HttpSendRequest" . Suffix, UPtr,hRequest,UPtr,Headers,"UInt",Strlen(Headers),UPtr,&POSTData,"UInt",(POSTDataLength <> "") ? POSTDataLength : Strlen(POSTData))
  Return, "Error sending request."

 VarSetCapacity(ReturnedHeaders,512,0), HeaderLen := 512
 Loop, 5
 {
  If (HeaderRequest := DllCall("WinINet\HttpQueryInfo" . Suffix,UPtr,hRequest,UInt,21,UPtr,&ReturnedHeaders,"UInt*",HeaderLen,"UInt",0))
   Break
  VarSetCapacity(ReturnedHeaders,HeaderLen,0)
 }
 If HeaderRequest
 {
  Loop, % HeaderLen / CharSize
  {
   If Not NumGet(ReturnedHeaders,(A_Index - 1) * CharSize,Char)
    NumPut(10,ReturnedHeaders,(A_Index - 1) * CharSize,Char) ;Asc("`n") = 10
  }
  VarSetCapacity(ReturnedHeaders,-1)
 }
 Else
  Return, "Timed out"

 Loop, Parse, ReturnedHeaders, `n, `r
 {
  RegExMatch(A_LoopField,"S)HTTP/1\.[01]\s+(\d+)",HTTPReturnValue)
  Break
 }

 ;HttpQueryOps handling
 If HTTPQueryOps <>
 {
  IfInString, HTTPQueryOps, ShowHeader ;show all returned headers
   MsgBox, %ReturnedHeaders%
  IfInString, HTTPQueryOps, StoreHeader ;store returned headers in a global variable
   HttpQueryHeader := ReturnedHeaders
  IfInString, HTTPQueryOps, UpdateSize ;store the size of the content in a global variable
  {
   If RegExMatch(A_LoopField,"\nContent-Length:\s*?(\d+)",HeaderSize)
    HttpQueryFullSize := HeaderSize1
   Else
    HttpQueryFullSize := "Size unavailable"
  }
 }

 If HTTPReturnValue1 Not In 100,200,201,202,302
 {
  HTTPReturnCodes = |100=Continue|101=Switching Protocols|102=Processing (WebDAV) (RFC 2518)|200=OK|201=Created|202=Accepted|203=Non-Authoritative Information|204=No Content|205=Reset Content|206=Partial Content|207=Multi-Status (WebDAV)|300=Multiple Choices|301=Moved Permanently|302=Found|303=See Other|304=Not Modified|305=Use Proxy|306=Switch Proxy|307=Temporary Redirect|400=Bad Request|401=Unauthorized|402=Payment Required|403=Forbidden|404=Not Found|405=Method Not Allowed|406=Not Acceptable|407=Proxy Authentication Required|408=Request Timeout|409=Conflict|410=Gone|411=Length Required|412=Precondition Failed|413=Request Entity Too Large|414=Request-URI Too Long|415=Unsupported Media Type|416=Requested Range Not Satisfiable|417=Expectation Failed|418=I'm a teapot (RFC 2324)|422=Unprocessable Entity (WebDAV) (RFC 4918)|423=Locked (WebDAV) (RFC 4918)|424=Failed Dependency (WebDAV) (RFC 4918)|425=Unordered Collection (RFC 3648)|426=Upgrade Required (RFC 2817)|449=Retry With|500=Internal Server Error|501=Not Implemented|502=Bad Gateway|503=Service Unavailable|504=Gateway Timeout|505=HTTP Version Not Supported|506=Variant Also Negotiates (RFC 2295)|507=Insufficient Storage (WebDAV) (RFC 4918)|509=Bandwidth Limit Exceeded|510=Not Extended (RFC 2774)|
  Temp1 := InStr(HTTPReturnCodes,"|" . HTTPReturnValue1 . "=")
  If Not Temp1
   Return, "Invalid return code"
  Temp1 += StrLen(HTTPReturnValue1) + 2, HTTPMessage := SubStr(HTTPReturnCodes,Temp1,InStr(HTTPReturnCodes,"|",False,Temp1) - Temp1), Result := HTTPReturnValue1 . " " . HTTPMessage
  Return, StrLen(Result)
 }

 TotalSize := 0, BytesRead := 0
 Loop
 {
  BufferIndex := A_Index
  VarSetCapacity(Buffer%BufferIndex%,1024,0)
  ReadFile := DllCall("wininet\InternetReadFile",UPtr,hRequest,UPtr,&Buffer%BufferIndex%,"UInt",1024,"UInt*",BytesRead)
  If (ReadFile && !BytesRead)
   Break
  Else
   TotalSize += BytesRead, SizeArray .= BytesRead . "|"
 }
 If InStr(HTTPQueryOps,"UpdateSize")
  HttpQueryCurrentSize := TotalSize
 SizeArray := SubStr(SizeArray,1,-1)

 VarSetCapacity(Result,TotalSize + 1,0)
 pData := &Result
 Loop, Parse, SizeArray, |
  DllCall("RtlMoveMemory",UPtr,pData,UPtr,&buffer%A_Index%,"UInt",A_LoopField), pData += A_LoopField

 IsText := RegExMatch(ReturnedHeaders,"iS)\nContent-Type:.*?\btext\b.*?(?:\bcharset=\K[\w-]+|$\K)",CharSet)
 pData := &Result
 ;check for a byte order mark
 If ((NumGet(Result) & 0xFFFFFF) = 0xBFBBEF) ;UTF-8 BOM: EF BB BF
  pData += 3, CharSet := "utf-8"
 Else If (NumGet(Result) & 0xffff) = 0xFEFF ; UTF-16 BOM: FF FE
  pData += 2, CharSet := "utf-16"
 If CharSet In utf-8,utf-16
 {
  If IsFunc("StrGet")
   Result := %StrGetFunc%(pData,CharSet)
  Else
  {
   If CharSet = utf-8
   {
    CharCount := DllCall("MultiByteToWideChar","UInt",65001,"UInt",0,UPtr,pData,"Int",-1,"UInt",0,"Int",0)
    VarSetCapacity(Temp1,CharCount << 1), DllCall("MultiByteToWideChar","UInt",65001,"UInt",0,UPtr,pData,"Int",CharCount,UPtr,&Temp1,"Int",CharCount << 1)
    DllCall("WideCharToMultiByte","UInt",0,"UInt",0x400,UPtr,&Temp1,"Int",CharCount,UPtr,&Result,"Int",CharCount,"UInt",0,"UInt",0), VarSetCapacity(Result,-1)
   }
   Else
   {
    CharCount := DllCall("WideCharToMultiByte","UInt",0,"UInt",0x400,UPtr,pData,"Int",-1,"UInt",0,"UInt",0,"UInt",0,"UInt",0)
    DllCall("WideCharToMultiByte","UInt",0,"UInt",0x400,UPtr,pData,"Int",CharCount,UPtr,&Result,"Int",CharCount,"UInt",0,"UInt",0), VarSetCapacity(Result,-1)
   }
  }
 }
 ;treat all unrecognized names as this system's ANSI code page.
 Else If (IsText && A_IsUnicode)
  Result := %StrGetFunc%(pData,"CP0")
 DllCall("WinINet\InternetCloseHandle",UPtr,hRequest), DllCall("WinINet\InternetCloseHandle",UPtr,hInternet), DllCall("WinINet\InternetCloseHandle",UPtr,hConnect), DllCall("FreeLibrary",UPtr,hModule)
 Return, TotalSize
}




Result:
Code:
---------------------------
test.ahk
---------------------------
515
"

Time: Mon, 08 Aug 11 12:22:22 -0700
Source ip: (removed)

Headers (Some may be inserted by server)
UNIQUE_ID = TkA3bq3sqvkAACEULXcAAAAD
HTTP_USER_AGENT = AutoHotkeyScript
HTTP_HOST = posttestserver.com
CONTENT_LENGTH = 8
HTTP_CACHE_CONTROL = no-cache
REMOTE_ADDR = (removed)
REMOTE_PORT = 64521
GATEWAY_INTERFACE = CGI/1.1
REQUEST_METHOD = POST
QUERY_STRING = dump
REQUEST_URI = /post.php?dump
HTTP_CONNECTION = close
REQUEST_TIME = 1312831342

Post Params:

== Begin post body ==
t"
---------------------------
OK   
---------------------------



Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 9th, 2011, 2:10 am 
Offline
User avatar

Joined: October 7th, 2006, 8:45 am
Posts: 3330
Location: Simi Valley, CA
gllen wrote:
I'm pretty lost trying to figure out the problem, anyone have any idea?

Does posttestserver.com accept text post data in UTF-16 format?

_________________
Ternary (a ? b : c) guide     TSV Table Manipulation Library
Post code inside [code][/code] tags!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 9th, 2011, 3:00 am 
Offline

Joined: November 22nd, 2006, 10:11 pm
Posts: 20
Hi VxE,

Quote:
Does posttestserver.com accept text post data in UTF-16 format?


I'm just using posttestserver for the example and I'm not that well versed with unicode stuff so I'm really not sure how to tell.

For my normal use, I never require the ability to post UTF-16 data. I've been using the non-ascii version of AHK with the old httpQuery function (which I assume is UTF-8?) and it's been working great.

I would like the ability to receive unicode html in the response, but I never need to send it.

Is there a modification I can make to just send UTF-8? I tried changing the HttpSendRequest and using "Str" instead of UPTR for POSTData and Headers. That didn't work.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 9th, 2011, 3:17 am 
Offline
User avatar

Joined: October 7th, 2006, 8:45 am
Posts: 3330
Location: Simi Valley, CA
gllen wrote:
Is there a modification I can make to just send UTF-8? I tried changing the HttpSendRequest and using "Str" instead of UPTR for POSTData and Headers. That didn't work.

HttpSendRequest does not convert the post data from UTF-16 (the format used internally by unicode versions of AHK) to any other format.

The major difference between UTF-16 and other formats is that it uses 2 bytes to represent each character in the string.

You can try HTTPRequest, which is similar to httpQuery [/shameless self promotion]

_________________
Ternary (a ? b : c) guide     TSV Table Manipulation Library
Post code inside [code][/code] tags!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 9th, 2011, 6:32 am 
Offline

Joined: November 22nd, 2006, 10:11 pm
Posts: 20
[VxE] wrote:

You can try HTTPRequest, which is similar to httpQuery [/shameless self promotion]


Awesome! Looks like a huge improvement, will check it out. TY


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 15th, 2011, 1:50 am 
Offline

Joined: August 4th, 2010, 5:59 am
Posts: 53
I have a problem with this funtion and I'm sitting with it for 2 hours..

All I want is to get html code of a main ahk forum website after my loggin in.
Code:
#noenv
html     := ""
URL      := "http://www.autohotkey.com/forum/index.php"
POSTData := "username=my_username&password=my_password"

length := httpQuery(html,URL,POSTdata)
varSetCapacity(html,-1)

MsgBox %html%
Return

#include httpQuery.ahk

Why does this not work? I receive empty string instead of html code.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 16th, 2011, 3:26 pm 
Offline
User avatar

Joined: August 23rd, 2010, 6:22 pm
Posts: 781
Location: Ontario, Canada
gllen wrote:
I'm not able to get post params working with Uberi's version from Feb 11th. It seems to be only sending the first character of the post body?


Sorry, I will no longer be maintaining my HTTPQuery version, as it has been superseded by the quite excellent HTTPRequest function by VxE. I've edited my original post to reflect this change.

_________________
AutoHotkey.net | GitHub

My default license.


Report this post
Top
 Profile  
Reply with quote  
 Post subject: HTTPQuery; big success
PostPosted: November 3rd, 2011, 9:31 pm 
Offline

Joined: November 3rd, 2011, 8:49 pm
Posts: 1
Location: Western US
I know that HTTPRequest has gotten a lot of press, but I've had much better success for my application with Uberi's HTTPQuery, and an amalgam of all the advice on this and other posts.

For anyone who may be searching for a working example, here is a script I put together that queries the United Parcel Service SOAP / WSDL API.

Code:
;--------------
;Begin SOAP / WSDL Request Section
;--------------

#include httpQuery.ahk

html := ""
url := "https://wwwcie.ups.com/ups.app/xml/Rate"  ;for test environment
;url := "https://onlinetools.ups.com/ups.app/xml/Rate"   ; for live use
httpQueryDwFlags := (0x00800000|0x00001000|0x00002000|0x00000100)


POSTData =
(Ltrim
<?xml version="1.0"?>
<AccessRequest xml:lang="en-US">
    <AccessLicenseNumber>123456789ABCDEFG</AccessLicenseNumber>
   <UserId>8dwfje4e756s4fesjes</UserId>
   <Password>getyourownpassword</Password>
</AccessRequest>
<?xml version="1.0"?>
<RatingServiceSelectionRequest xml:lang="en-US">
  <Request>
   <RequestOption>Shop</RequestOption>
  </Request>
    <PickupType>
      <Code>03</Code>
   </PickupType>
  <Shipment>
    <Shipper>
      <ShipperNumber>123AB4</ShipperNumber>
      <Address>
        <PostalCode>98642</PostalCode>
        <CountryCode>US</CountryCode>
      </Address>
    </Shipper>
    <ShipTo>
      <Address>
        <PostalCode>%ShipToZip%</PostalCode>
        <CountryCode>US</CountryCode>
      </Address>
    </ShipTo>
    <ShipFrom>
      <Address>
        <PostalCode>98642</PostalCode>
        <CountryCode>US</CountryCode>
      </Address>
    </ShipFrom>
     <Service>
          <Code>02</Code>
     </Service>
     <PaymentInformation>
            <Prepaid>
              <BillShipper>
                   <AccountNumber>123AB4</AccountNumber>
              </BillShipper>
            </Prepaid>
     </PaymentInformation>
     <Package>
           <PackagingType>
              <Code>02</Code>
            </PackagingType>
            <PackageWeight>
              <UnitOfMeasurement>
                 <Code>LBS</Code>
               </UnitOfMeasurement>
              <Weight>%RoundedTotalWeight%</Weight>
            </PackageWeight>   
      </Package>
  </Shipment>
</RatingServiceSelectionRequest>
)


length := httpQuery(html,URL,POSTData)
varSetCapacity(html,-1)

 
FileAppend, %html%, C:\Documents and Settings\Administrator\Desktop\upstest5.txt


Obviously, this is just a section of the entire script as evidenced by the variables in the XML above, but this seemed to be the relevant portion to this thread.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 233 posts ]  Go to page Previous  1 ... 12, 13, 14, 15, 16

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bon and 13 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group