Help with a HTTPSocket

If I write in any browser http://xxx.xxx.xxx/hgwmv/update.php (xxx.xxx.xxx is my server) I get the answer that the php file produces.
But if I code:

Dim http As New HTTPSocket http.Yield = True Dim tex As String = http.Get("http://xxx.xxx.xxx/hgwmv/update.php", 5)
what I get is:

[quote]

403 Forbidden

Forbidden

You don't have permission to access /hgwmv/update.php on this server.

[/quote] Why do I have permission from any browser and I don't have from inside Xojo?

Hypothesis:

If you access this through the browser, it executes the PHP script.
If you use an httpsocket, it tries to download the php file itself.

Random suggestion:
Maybe try POST instead of GET?

Thanks Jeff,

Unfortunately there is the same problem with POST.
Perhaps I must create a .htaccess file in the folder to give this permission.

If I create an HTMLViewer1

HTMLViewer1.LoadURL "http://xxx.xxx.xxx/hgwmv/update.php"

it works.
But can I read the text on this HTMLViewer1?

Christian has plugins to get the HTML code within an HTMLViewer. Then you clean it up of the HTML code and you are fine.

Use a browser user-agent

Assuming the response even has any HTML wrappings…

Indeed.

Thanks for your suggestions.[quote=274440:@Michel Bujardet]Christian has plugins to get the HTML code within an HTMLViewer. Then you clean it up of the HTML code and you are fine.[/quote]
Cleaning it up is not a problem, but it is a pity to have to install a pluging just to read something that Xojo must know since it was able to write it on the HTMLViewer.
There is a method “LoadPage” but unfortunately there is no method to get the shown code, something like “SavePage”. That is something we usually do in the browser by right-clicking on the page.

Try Ashot’s suggestion. Some sites will not respond to requests that do not include a User Agent, probably to help thwart robots.

[quote=274423:@Jeff Tullin]If you access this through the browser, it executes the PHP script.
If you use an httpsocket, it tries to download the php file itself.
[/quote]

Jeff, that’s not how PHP works. PHP scripts are executed on the server side, not the client side. With a properly-working server, you can never access the contents of a PHP file directly.

Thats kinda what I was saying. :slight_smile:

When accessed via a browser, the PHP scrip is executed on the server side and returns data. Yep.

I was just wondering if using an HTTPSocket was being treated as a request to download the PHP as a file, and getting

403 Forbidden as a response. ie 'you can never access the contents of a PHP file directly.'

It’s not up to the client side (browser, HTTPSocket, curl, etc.), it’s entirely up to the server.

You’re definitely missing some header stuff as others have suggested. It’s either the missing user agent or the script is being limited to an http 1.1 request and the regular HTTP socket still makes an http1.0 request. The full set of headers when I connect to something with safari that are sent is:

GET /something/else/yes HTTP/1.1
Host: 192.168.0.10:20301
Accept-Encoding: gzip, deflate
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/601.6.17 (KHTML, like Gecko) Version/9.1.1 Safari/601.6.17
Accept-Language: en-us
DNT: 1
Connection: keep-alive

You can add the user agent to it with the SetRequestHeader before you make the request like:

http.SetRequestHeader(“User-Agent:”, "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/601.6.17 (KHTML, like Gecko) Version/9.1.1 Safari/601.6.17”)

If that still doesn’t work then it might be the http 1.0 vs 1.1 problem in which case you will have to use the new framework to make the request. which should work on most targets now I think:

http://developer.xojo.com/xojo-net-httpsocket

It’s very similar but sends a 1.1 request. You can also roll your own easily enough as long as you don’t need to support any of the drastically more complicated encodings or something. Just to get data back isn’t that hard with a raw socket and sending that list of headers.

James: That’s perfect! It works!!

Ashot: You were right, but I did not understand you. My knowledge is limited :frowning: