﻿B4J=true
Group=Default Group
ModulesStructureVersion=1
Type=Class
Version=9.3
@EndOfDesignText@
Sub Class_Globals
	Private gHuc As JavaObject
	
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize(url As String,method As String)
	If url.StartsWith("https") Then
		gHuc = (Me).As(JavaObject).RunMethod("openConSSL",Array(url))
	Else
		gHuc = (Me).As(JavaObject).RunMethod("openCon",Array(url))
	End If
	gHuc.RunMethod("setFollowRedirects",Array(True))
	gHuc.RunMethod("setRequestMethod",Array(method))
End Sub

public Sub setTimeOut(milis As Long)
	gHuc.RunMethod("setConnectTimeout",Array(milis))
End Sub

public Sub postText(str As String,encoding As String)
	postBytes(str.GetBytes(encoding))
End Sub

public Sub postBytes(by() As Byte)
	gHuc.RunMethod("setDoOutput",Array(True))
	Dim ots As OutputStream = gHuc.RunMethod("getOutputStream",Null)
	ots.WriteBytes(by,0,by.Length)
	ots.Flush
	ots.Close
End Sub

public Sub getResponseAsText(encoding As String) As String
	Dim sb As TextReader
	sb.Initialize2(gHuc.RunMethod("getInputStream",Null),encoding)
	Dim response As String = sb.ReadAll
	sb.Close
	Return response
End Sub

public Sub getResponseAsBytes As Byte()
	Dim ins As InputStream = gHuc.RunMethod("getInputStream",Null)
	Dim by() As Byte
	ins.ReadBytes(by,0,ins.BytesAvailable)
	Return by
End Sub

public Sub getResponseCode As Int
	Return gHuc.RunMethod("getResponseCode",Null)
End Sub

public Sub setRequestProperty(key As String, value As String)
	gHuc.RunMethod("setRequestProperty",Array(key,value))
End Sub

#if java
import javax.net.ssl.HttpsURLConnection;
import java.net.HttpURLConnection;
import java.net.URL;

public static HttpsURLConnection openConSSL(String spec) throws Exception {
	URL url = new URL(spec);
	HttpsURLConnection con = (HttpsURLConnection)url.openConnection();	
	return con;
}

public static HttpURLConnection openCon(String spec) throws Exception {
	URL url = new URL(spec);
	HttpURLConnection con = (HttpURLConnection)url.openConnection();	
	return con;
}
#End If