http.request giving blank...

i have a question about http.request, it gets blank for some reason on this request, any ideas?

local http = require(“socket.http”)
local ltn12 = require(“ltn12”)

native.setActivityIndicator(true);
local path = system.pathForFile( “mos.content”, system.DocumentsDirectory )
local myFile = io.open( path, “w+b” )

local content=http.request{
url = “http://www.google.com”,
sink = ltn12.sink.file(myFile),
}
native.showAlert(content,content);

it displays 1 in the popup, but the file contents nothing at all… thanks!

This is on OS Beta 4 [import]uid: 6317 topic_id: 1109 reply_id: 301109[/import]

sorry i had the wrong URL in there. the url i’m trying to get content from is

url = “http://ws.unicornlabs.com?hk=9871&appId=1350&udid=000&service=launchAd”,

Google seems to work ok. and this url works in a browser, thanks! [import]uid: 6317 topic_id: 1109 reply_id: 2830[/import]

Am pretty sure that you may need to escape the “&” character and off the top of my head I can’t recall.

let me see what I can do

carlos [import]uid: 24 topic_id: 1109 reply_id: 2831[/import]

thanks, tried adding \ before the & symbols and made no difference though. [import]uid: 6317 topic_id: 1109 reply_id: 2833[/import]

if i do this

url = “http://ws.unicornlabs.com

it returns 200 which is ok

but if i do this

url = “http://ws.unicornlabs.com?hk=9871&appId=1350&udid=000&service=launchAd

I get host not found

Hmmmm…

C [import]uid: 24 topic_id: 1109 reply_id: 2835[/import]

Is it a bug with dynamic URLs? [import]uid: 6317 topic_id: 1109 reply_id: 2836[/import]

Hmmm… could be that it is re-routing… and can’t figure out the re-route…

C [import]uid: 24 topic_id: 1109 reply_id: 2838[/import]

is there any other way of downloading the contents of a dynamic URL with corona? [import]uid: 6317 topic_id: 1109 reply_id: 2839[/import]

This is not a bug. I think it’s just not clear how to use Lua socket and http requests. 

First, you need to add a forward slash after .com and before your ?.  This is why the host is not found. After doing this you should see the file in your Documents directory containing the web page content.  

Try changing sink = ltn12.sink.file(myFile) to sink = ltn12.sink.file(io.stdout). This will print the output to the terminal. Make sure you open the simulator using Corona Terminal. This is a good way to verify your data is coming through. 

To show something in the ui popup I’d recommend reading that from the file that gets saved in the Documents directory, using something like: 

local content = io.open(path, “r”)

I think you’re getting a “1” as the content of your http.request because it’s the first value passed back in the headers, not the content itself.  You have to take a few more steps to get content, such as what I recommend above–read it from the file that gets saved out as a result of the request.    

Here’s more info:    

http://w3.impa.br/~diego/software/luasocket/http.html

-Gilbert
[import]uid: 5917 topic_id: 1109 reply_id: 2840[/import]

I added the forward slash where you suggested and I get this when I put io.stdout instead of myFile (Which is open). When I leave my file pointer in there it puts blank into it. If i use google however, it still puts the correct content in that file.



[import]uid: 6317 topic_id: 1109 reply_id: 2841[/import]

Hi,

Sorry I didn’t include any code in my last message. I was on my iPhone, moving between modes of transportation. :wink:

Try this code. I rewrote some of the stuff in your code:

local http = require("socket.http")  
local ltn12 = require("ltn12")  
  
--the url needed a forward slash ( / ) after unicornlabs.com  
myUrl = "http://ws.unicornlabs.com/?hk=9871&appId=1350&udid=000&service=launchAd"  
  
--native.setActivityIndicator(true)  
local path = system.pathForFile( "mos.content", system.DocumentsDirectory )  
local myFile = io.open( path, "w+b" )  
  
--Check for a connection to the host  
r, c, h = http.request{  
 method = "HEAD",   
 url = myUrl  
}  
  
local hostFound = false  
if c == 200 then  
 print("host found!")  
 hostFound = true  
else   
 print("host not found")  
end   
  
--If a connection with the host was found, get the content  
if hostFound then  
 http.request{  
 url = myUrl,  
 sink = ltn12.sink.file(myFile)  
 -- sink = ltn12.sink.file(io.stdout)  
 }  
  
 --Open the file containing the content we received  
 local file = io.open(path, "r")  
 local content = file:read("\*a")  
  
 --Show the content in the terminal  
 print(content)  
  
 --Show the content  
 native.showAlert(content,content)  
end  
  

I added a few lines to check the page headers just to see if a connection to the host was made. If a connection is made, then the script can go on and get the content. It gets the content, puts it in a file (which is what sink does in the http.request), and then it reads the content from the file and shows it in the alert.

Notice I also commented out activity indicator. If you look at the documentation, API Reference p.42, you’ll notice that the activity indicator doesn’t actually come up until the end of your script. So I think it was coming up even after you made a connection and got your content and then it was not going away. A way to get this to work might be to use an event listener to initiate the connecting to the server and getting the content. This way the whole script can run first and then when it’s done the event listener kicks in the connection. See p.56 in the API Reference for some examples of event listeners.

Lastly, the content you’re grabbing from the web has html table tags, image tags, and other formatting. You won’t be able to display these in the alert. But you probably already know that.

Let me know if you need any more help with this.

-Gilbert

[import]uid: 5917 topic_id: 1109 reply_id: 2847[/import]

it works, thanks a ton! Now last question I have is that i am putting this content into a webpopup, is there any way to rotate the webpopup based on current rotation? i don’t want to rotate anything else on the screen (entire app) just the webpopup. thanks! [import]uid: 6317 topic_id: 1109 reply_id: 2848[/import]

Use beta4 and a build.settings file:

[code]
– build.settings

settings =
{
orientation =
{
default = “portrait”,
content = “portrait”,
supported =
{
“portrait”, “portraitUpsideDown”,
},
},
}
[/code] [import]uid: 5712 topic_id: 1109 reply_id: 2851[/import]

I had tried modifying build.settings file with these type of settings, but the webpopup for some reason does not rotate when i rotate the ipad. Is this iphone only? [import]uid: 6317 topic_id: 1109 reply_id: 2853[/import]

Never mind, it does not work. Webpopup is not support by autorotating. It can’t be rotated. :frowning: [import]uid: 5712 topic_id: 1109 reply_id: 2854[/import]

I was going to release a lite version of my app with admob support. Guess not :frowning: [import]uid: 5712 topic_id: 1109 reply_id: 2855[/import]

yeah this is a huge hinderance, if any of the devs are reading is this on the “to work on” list for next beta? would be HUGELY useful! Thanks! [import]uid: 6317 topic_id: 1109 reply_id: 2856[/import]

Have you seen Darren Osadchuk’s code for getting Admob to work in Corona using a web popup?

http://www.ludicroussoftware.com/corona/admob-ad-support-in-corona/

I haven’t tried it yet, but I’ve seen it on a device and it looks good.

-Gilbert [import]uid: 5917 topic_id: 1109 reply_id: 2857[/import]

Gilbert,

we have seen it and wanted to use it, but can’t because it doesn’t rotate when the orientation is changing. And handling orientation for at least 2 of them is basically mandatory these days in the app store. And that goes even more for the IPad.

Michael [import]uid: 5712 topic_id: 1109 reply_id: 2863[/import]

Can you just destroy the webpopup on an orientation change and recreate it in the new orientation?

The guideline for iPad is that if you run in landscape, you should generally try to support BOTH landscape modes (and the same for portrait); the idea is that the app should work in the direction the user is currently holding the device. But in most cases the user is not going to be rotating the device by 180 degrees very often in a given session, so even if there’s a momentary lag when recreating the ad banner upside-down, it may be acceptable? (I haven’t tested this.) [import]uid: 3007 topic_id: 1109 reply_id: 3024[/import]