Trouble with HTTP Post

lagore

Active Member
Licensed User
Longtime User
Hi
I am currently converting a program which I have written in VB.net to run on Android using Basic4Android but I am not having any success with the POST method.
The bit of code in VB.net that works is,

B4X:
Dim postData As String = "username=myusername&password=mypassword&back=https%3A%2F%2Fecrew.mysite.com%2Fwtouch%2Fwtouch.exe%2Findex%3FMAC%3D0%26VER%3D0"
        Dim tempCookies As New CookieContainer
        Dim encoding As New UTF8Encoding
        Dim byteData As Byte() = encoding.GetBytes(postData)

        Dim postReq As HttpWebRequest = DirectCast(WebRequest.Create("https://portal.mysite.com/weblogin/bin/weblogin.cgi"), HttpWebRequest)
        postReq.Method = "POST"
        postReq.KeepAlive = True
        postReq.CookieContainer = tempCookies
        postReq.ContentType = "application/x-www-form-urlencoded"

        postReq.Referer = "https://portal.mysite.com/weblogin/bin/weblogin.cgi"
        postReq.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.11) Gecko/20101012 Firefox/3.6.11 GTB7.1 ( .NET CLR 3.5.30729)"
        postReq.ContentLength = byteData.Length

        Dim postreqstream As Stream

        Try
            postreqstream = postReq.GetRequestStream()

        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error Logging on to the mysite Portal")
            Error_Writer("mysite Portal Initial Login" & vbCrLf _
                         & ex.Message & vbCrLf)
            Return
        End Try
        postreqstream.Write(byteData, 0, byteData.Length)

        postreqstream.Close()
        Dim postresponse As HttpWebResponse



        postresponse = DirectCast(postReq.GetResponse(), HttpWebResponse)

My code in Basic4Android is

B4X:
Sub Process_Globals
          Dim hc As HttpClient
End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
        hc.Initialize("hc")
      End If
   
   postData = "username=myusername&password=mypassword&back=https%3A%2F%2Fecrew.mysite.com%2Fwtouch%2Fwtouch.exe%2Findex%3FMAC%3D0%26VER%3D0"
   PostUrl = "https://portal.mysite.com/weblogin/bin/weblogin.cgi"

   Dim str() As Byte
   
   str = postData.GetBytes("UTF8")
   
       Dim req As HttpRequest
   req.InitializePost2(PostUrl, str)
   req.SetContentType("application/x-www-form-urlencoded")
   req.SetContentEncoding("UTF8Encoding")
   req.Timeout=45000
 
       hc.Execute(req, 1)
End sub

When I run the code I get the following error

Error. Url=https://portal.mysite.com/weblogin/bin/weblogin.cgi Message=java.net.UnknownHostException: portal.mysite.com

Any ideas what I need to change or am I trying to do something that either Android or Basic4Android cannot do. The long timeout is required as the site can be slow to respond.
Am I correct in stating that the Android OS takes care of cookies in the background so that is one aspect I do not have to worry about.
Unfortunately neither bits of code will not work if you paste them into the IDE as I have changed/removed any sensitive information.

Edward
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
I recommend you to try HttpUtils, it is simpler and safer to use.
Cookies are handled automatically by the underlying Http implementation.

However this error means that the device has failed to find the IP address of this URL. Are you sure that the device is connected to the internet?
Can you connect to other sites?
 
Upvote 0

lagore

Active Member
Licensed User
Longtime User
POST using HttpUtils

Thanks for quick reply Erel
I managed to get the device to interact with the web site, not getting the correct response but maybe the way it has formatted the POST string is the problem now, the problem was the emulator was not accessing the web (thought it could). I have started trying HttpUtils to sort the POST problem but I need to increase the timeout how do i do that with HttpUtils.
Edward
 
Upvote 0

lagore

Active Member
Licensed User
Longtime User
Time out in HttpUtilsService

I have sorted out all my HTTP POST problems. HttpUtils works very well, I had to put a timeout entry into HttpUtilsService as it was timing out after 30 sec and the website I am logging onto takes over 40sec to respond, so it does appear that HttpUtils does have a time out of approx 30sec by default.
Edward
 
Upvote 0

vasper

Member
Licensed User
Longtime User
I have a similar problem, but I can't seem to get the timeout to more than 20 seconds. Setting to 5000 milliseconds times out in 5 seconds, but all the numbers above 20000 timeout in just under 20 seconds!
 
Upvote 0

vasper

Member
Licensed User
Longtime User
I tried it. Again, a 20 second timeout. This is the code I changed:

B4X:
If Post Then
      If PostInputStream.IsInitialized Then
         req.InitializePost(link, PostInputStream, PostLength)
         req.Timeout = 0
      Else
         req.InitializePost2(link, PostBytes)
         req.Timeout = 0
      End If
   Else
      req.InitializeGet(link)
      req.Timeout = 0
   End If

I worked around the problem by first loading the url with an invisible webview and on the pagefinished event I called the Httputils procedure thus using the cached page to get my data without a timeout.
 
Last edited:
Upvote 0

vasper

Member
Licensed User
Longtime User
I see no reason why HttpUtils should be the problem. It is quite straight forward. But then again, I would like to find out what the problem is so I will do what you suggest. Thanks again.
 
Upvote 0

lagore

Active Member
Licensed User
Longtime User
I thought that setting the timeout to 50000 sorted the problem but in fact it has not, if I use the app on wifi it works because it get a response before the 20sec timeout, when using the data connection it always times out because of the added delay in the cell connection. I get the same results as vasper a timeout of 0 or anything above 20000 gives a max of 20 sec, not enough for me. Any thoughts?
Edward
 
Upvote 0

lagore

Active Member
Licensed User
Longtime User
In the 'JobDone' sub on the Post return
B4X:
Case "POST Job1"         'Portal Login
   If HttpUtils.IsSuccess(PostUrl) Then
it comes back as not success, I am not sure if that is during the request or while reading the response! I presume it is the response but am not familiar with the inner workings of HttpUtils.
Edward
 
Upvote 0
Top