xsharp.eu • GetDocumentByGetOrPost
Page 1 of 1

GetDocumentByGetOrPost

Posted: Tue Oct 26, 2021 3:25 am
by ny
I have an old vo 2.6/2.8 app and after a weeks work it compiles fine in XSharp (via the voxporter). However when I run it I get this runtime error pointing to a problem in GetDocumentByGetOrPost :

Description : Conversion Error from USUAL (STRING) to PTR
Subsystem : BASE
GenCode : EG_DATATYPE Data type error
FuncSym : USUAL => PTR
Severity : ES_ERROR
Can Default : False
Can Retry : False
Can Substitute : False
Argument Type : PTR
Argument Number : 1
Argument : USUAL
Arguments : {{}}
Expected Argument Type : System.IntPtr
Stack Trace :
__USUAL:OP_IMPLICIT (Line: 0)
MYHTTP2:GETDOCUMENTBYGETORPOST (Line: 94)

Line 94 (from Norbert's GetDocumentByGetOrPost) is:

IF HttpSendRequest(SELF:hRequest, ;
NULL_PTR, ;
0, ;
PTR(_CAST, cData), ;
nDataLen)

I am sure there are newer ways to do this with .net but my initial aim is to get the same code working in both VO and XSharp.

Regards, Neale

GetDocumentByGetOrPost

Posted: Tue Oct 26, 2021 11:29 am
by wriedmann
Hi Neale,
please do yourself a favor and replace this code with newer code using the .NET classes - it works a lot better.
I have attached a prg file that I'm using for this.
Wolfgang

Code: Select all

method GetDocumentByForm( cURL as string, cData as string, nPort as int ) as byte[]
	local aResult as byte[]
	local oRequest as System.Net.HttpWebRequest // System.Net.WebRequest
	local oCredentials as System.Net.NetworkCredential  
	local oStream as Stream
	local oResponse as System.Net.HttpWebResponse // System.Net.WebResponse
	local aData as byte[]
	local nDataLen as int
	
	aResult := byte[]{ 0 }	
	oRequest := ( System.Net.HttpWebRequest ) System.Net.WebRequest.Create( cURL )
	if ! String.IsNullOrEmpty( _cUserName )
		oCredentials := System.Net.NetworkCredential{ _cUserName, _cPassword }
		oRequest:Credentials := oCredentials
	endif
	aData := Encoding.ASCII:GetBytes( cData )
	nDataLen := aData:Length
	System.Diagnostics.Debug.WriteLine( "http data:" + cData + "|" + "Length:" + nDataLen:ToString())
	oRequest:@@Method := "POST"                                           
	oRequest:Accept := "*/*"
	oRequest:ContentType := "application/x-www-form-urlencoded"
	oRequest:ContentLength	:= nDataLen
	oStream := oRequest:GetRequestStream()
	oStream:Write( aData, 0, nDataLen )
	oStream:Close()
	oResponse := ( System.Net.HttpWebResponse ) oRequest:GetResponse()
	oStream := oResponse:GetResponseStream()
	aResult := self:GetBytes( oStream )
	oStream:Close()
	return aResult
httpBase.zip
(1.53 KiB) Downloaded 43 times

GetDocumentByGetOrPost

Posted: Tue Oct 26, 2021 12:01 pm
by Karl-Heinz
Hi Neale,

doing something like

Code: Select all

PTR(_CAST, cData)
is a very bad idea !

try this:

Code: Select all

LOCAL pData AS PTR

[...]

	pData := MemAlloc ( nDataLen)  // assuming that nDataLen already holds the length of cData !
    
	MemCopyString ( pData , cData , nDataLen ) 

	HttpSendRequest(SELF:hRequest, ;
		NULL_PTR, ;
		0, ;
		pData, ;
		nDataLen) 

	Memfree ( pData ) 

[...]


if it works with X# - i expect it does ;-) -, apply the same changes to the VO side.

regards
Karl-Heinz

GetDocumentByGetOrPost

Posted: Tue Oct 26, 2021 7:57 pm
by ny
Thanks for both replies. I am also looking at winHTTPCall - very new to this and still very attached to VO having used it for so long.

Regards, Neale

GetDocumentByGetOrPost

Posted: Tue Oct 26, 2021 9:21 pm
by ic2
Hello Neale,

I am not sure what you want to do with it? Here's another option which returns the content of a URL cPage (I used it for retrieving Teletext =Ceefax in the UK pages):

Dick

Code: Select all

Method GetFileViaHttp(cPage As String) As String // class TeletekstIC2
//#s General function to get content of page via http
Local myRequest As System.Net.HttpWebRequest
Local myResponse As System.Net.WebResponse
Local oCachePol As System.Net.Cache.HttpRequestCachePolicy
Local sr As System.IO.StreamReader
Local cResult As String
Try
	
myRequest := (System.Net.HttpWebRequest)(System.Net.WebRequest.Create(cPage))
myRequest:Proxy:=Null   // Otherwise it takes 7 seconds the 1st time this is called
myRequest:Timeout:=2000 // Only try update server 2 seconds 12-4-2015
myRequest:@@METHOD:="GET"
myResponse := myRequest:GetResponse()  // This takes very long if server is not available
sr := System.IO.StreamReader{myResponse:GetResponseStream(), System.Text.Encoding.GetEncoding("ISO-8859-1")}    // 4-5-2017, ISO instead of UTF8 probably not needed but to be sure
cResult := sr:ReadToEnd()
sr:Close()
myResponse:Close()
Catch _Exception As System.Exception
		cResult:=""  // When it's gone wrong, return "" was: assume 404 error
End Try
Return  cResult