Is it possible to get the user-agent from webView object?

Does anyone know if there is a way to “grab” the user-agent from a webView object? When I instantiate a webview I need to grab the user-agent and store it in a variable that I can later use to override the user-agent field in an HTTP request before I make a network.request() to a particular server.

I’ve searched through Corona’s documentation and haven’t seen anything to answer my question. Neither do I see anything on the forums concerning this as well.

Thanks for any help!

Guy

I don’t believe you can…

Rob

I was afraid of that.

Thanks for answering.

Guy

This may not be possible for you if you don’t control your own web service, but you could craft a php page that just returns the user-agent header in the response body and request that page before your webView request. I haven’t looked, but there may even be publicly available pages that return the user-agent and other request headers.

Hmm, php looks like it would do what I need. However, I don’t know any php but I looked on the internet and found this. At first glance it looks like it would print out what I need??? If so, perhaps I can save the user-agent to a lua variable?

\<?php echo $\_SERVER['HTTP\_USER\_AGENT'] . "\n\n"; $browser = get\_browser(null, true); print\_r($browser); ?\>

Also, I’m not sure how to incorporate php into lua. I looked on the corona forums and didn’t really find an answer. So, how does one incorporate php into corona?

Thanks for any guidance possible,

Guy

PHP cannot be hosted in Corona – it needs to live on a web server that supports PHP. If you have a web server, you would create a file on it and then request it from Corona using network.request().

Example:

getUserAgent.php
[lua]<?php
echo $_SERVER[‘HTTP_USER_AGENT’];
?>[/lua]

That’s all you need to put on the web server. Then, in your Corona code, you would do this to get the user agent string into a variable:
[lua]local userAgent --variable that will store the user-agent string

local function networkListener( event )

if ( event.isError ) then
print( “Network error!” )
else
userAgent = event.response
print ( "RESPONSE: " … event.response )
end
end

network.request( “http://your website.com/getUserAgent.php”, “GET”, networkListener )[/lua]

Again, this is only possible if you have access to a web server that supports a scripting language such as PHP. Alternatively, there may already be a page publicly hosted somewhere on the internet that does this, but I have not looked.

Thanks! This is exactly what I need. It makes sense now. Thanks for the explanation!

Guy

My only concern is that the UA for a webView could be different than the UA for a network.requests() (in particular since you can set the UA for network request!)

Rob

Thanks Rob, 

You are correct. I eventually figured that out. I needed to send the UA from a cell phone to a remote server. Apparently IOS doesn’t send a proper UA. I eventually had to create my own UA to send by retrieving different system info and piecing them together to make a UA that the server would accept.

Guy

I don’t believe you can…

Rob

I was afraid of that.

Thanks for answering.

Guy

This may not be possible for you if you don’t control your own web service, but you could craft a php page that just returns the user-agent header in the response body and request that page before your webView request. I haven’t looked, but there may even be publicly available pages that return the user-agent and other request headers.

Hmm, php looks like it would do what I need. However, I don’t know any php but I looked on the internet and found this. At first glance it looks like it would print out what I need??? If so, perhaps I can save the user-agent to a lua variable?

\<?php echo $\_SERVER['HTTP\_USER\_AGENT'] . "\n\n"; $browser = get\_browser(null, true); print\_r($browser); ?\>

Also, I’m not sure how to incorporate php into lua. I looked on the corona forums and didn’t really find an answer. So, how does one incorporate php into corona?

Thanks for any guidance possible,

Guy

PHP cannot be hosted in Corona – it needs to live on a web server that supports PHP. If you have a web server, you would create a file on it and then request it from Corona using network.request().

Example:

getUserAgent.php
[lua]<?php
echo $_SERVER[‘HTTP_USER_AGENT’];
?>[/lua]

That’s all you need to put on the web server. Then, in your Corona code, you would do this to get the user agent string into a variable:
[lua]local userAgent --variable that will store the user-agent string

local function networkListener( event )

if ( event.isError ) then
print( “Network error!” )
else
userAgent = event.response
print ( "RESPONSE: " … event.response )
end
end

network.request( “http://your website.com/getUserAgent.php”, “GET”, networkListener )[/lua]

Again, this is only possible if you have access to a web server that supports a scripting language such as PHP. Alternatively, there may already be a page publicly hosted somewhere on the internet that does this, but I have not looked.

Thanks! This is exactly what I need. It makes sense now. Thanks for the explanation!

Guy

My only concern is that the UA for a webView could be different than the UA for a network.requests() (in particular since you can set the UA for network request!)

Rob

Thanks Rob, 

You are correct. I eventually figured that out. I needed to send the UA from a cell phone to a remote server. Apparently IOS doesn’t send a proper UA. I eventually had to create my own UA to send by retrieving different system info and piecing them together to make a UA that the server would accept.

Guy