Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal

Archived

This topic is now archived and is closed to further replies.

  • 0
Xylem

Request and read from a HTTPS website

Question

So i started messing about with the internet card, i can get onto google just fine as long as i use http with port 80. But when i try to use anything with a https and port 443 i can't get anything. I keep getting an invalid port or url address. Im pretty sure my address is right but i think my port might be wrong

Share this post


Link to post
Share on other sites

6 answers to this question

Recommended Posts

Yes, that's right. internet.open creates a TCP socket. They are useful when you need to communicate with a non-HTTP server (you probably know things like SSH, FTP -- those are all non-HTTP protocols).

But when you want to send a request to an HTTP server, you need to use internet.request. It's a nice wrapper for component.internet.request that allows to interate over the response body. Like this:

local inet = require("internet")

local response = inet.request("https://google.com")
local body = ""
for chunk in response do
  body = body .. chunk
end

print(body)

Chunk is a small piece of response, one, maybe a few (I can't remember) kilobytes in size, or even empty. If a connection problem occurs, an error is thrown.

No one forbids you to use sockets to connect to an HTTP server. I do that in my simple http library. But that is slow and requires a TLS library with many dependencies when you want to use HTTPS. It can be useful (you can set the request method and control exactly what happens during the connection), although I recommend to use internet.request.

And, finally, internet.request supports HTTP and HTTPS.

Share this post


Link to post
Share on other sites

What did you run? Was it require("internet").request("https://google.com:443"), or require("internet").connect("https://google.com:443"), or require("component").internet.request("https://google.com:443"), or require("component").internet.connect("https://google.com:443"), or what?

Share this post


Link to post
Share on other sites

This is the snippet of code i used

internet = require("interenet")

body = internet.open("https://www.google.com", 443)

print(body)

 

Share this post


Link to post
Share on other sites

I don't think you're using the correct function, internet.open creates a TCP socket with the IP at the port. Try using internet.request, although OC may not support HTTPS, pretty sure computercraft doesn't.

Share this post


Link to post
Share on other sites

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.