הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Saturday, September 6, 2014 6:30 PM
I have seen a couple of posts on setting the UserAgent when using the WebBrowser's .Navigate method with something like:
WebBrowser1.Navigate("http://example.com", "_top", Nothing, "UserAgent: Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1C25 Safari/419.3")
My issue does not involve the browser control. I have visual basic WinForm application (VS2010) that loads images and flash files (with AxShockwaveFlash control) from my web serve.
To load flash files:
flashDisplay = New AxShockwaveFlashObjects.AxShockwaveFlash()flashDisplay.Movie = "http://example.com/flash.swf"
To load images:
Dim pic As New PictureBox With {.ImageLocation = "http://example.com/image.png"}
pic.load()
On my server logs, the UserAgent for these calls show as something like:
Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.2; .NET4.0C; .NET4.0E)
This is making it quite difficult to separate App traffic from my regular web traffic, and even harder to understand which versions of the App are being serviced by the web server. How can I set the UserAgent of my App to something like "AppName version X.X" ?
Thanks,
-Patrick
All replies (10)
Saturday, September 6, 2014 6:59 PM
can you try following.
WebBrowser wc = new WebBrowser();
wc.Navigate("http://google.com", null, null, "User-Agent: User agent");
Tech
Saturday, September 6, 2014 7:04 PM
Understanding the IE User Agent String
Understanding user-agent strings
La vida loca
Sunday, September 7, 2014 12:13 AM
Hi.
I mentioned in my post that I am not using the WebBrowser control. I am aware of the wc.Navigate method (I also mention it in my post), so I am not sure I understand your solution. Would you elaborate please?
-Patrick
Sunday, September 7, 2014 12:20 AM
Mr. Monkeyboy,
Thanks for your post. You provided some good links about how to read and make sense of User Agent strings. My issue was not how to understand the User Agent, but rather how to change how it is reported when my App accesses a web resource on my server. Your links don't seem to address the issue I raised so maybe I missed the point you were trying to make?
One interesting tidbit from your 2nd link is that the User Agent can be altered by changing some value in the registry. This would not be a solution specific to my App though, or specific to calls made to my server. I don't think it would be good practice to make a permanent change to how the user's machine reports itself to all servers all the time.
I would appreciate your thoughts, beyond the links!
-Patrick
Sunday, September 7, 2014 1:06 AM
If you read the links you will note the user agent is not for providing information you desire to provide in it.
At least I didn't see a field for "AppName version X.X". And if you added that how is a website supposed to interpret that?
According to the links I provided the following information is what a user agent string is for.
"user agent string, which identifies your browser and provides certain system details to servers hosting the websites you visit"
I suppose you could alter appropriate registry entries and see what happens.
La vida loca
Sunday, September 7, 2014 3:06 AM
Mr. Monkeyboy
I think what I am asking is in line with what the user agent is meant to do. To provide my web server with the details it needs to properly handle http requests from the App. To be clear I was not inquiring about how to change the user agent reported to any other website. I was looking for a way that my application installed locally could identify itself to the webserver when (for example) it accesses http://example.com/image.png.
As I understand it, web servers make use of the user-agent string to have some information about the nature of the client making the http request. Isn't that what I am trying to accomplish?
I'm not sure changing the registry is a good idea because it might change how the system reports itself to all websites, not just mine
I thank you for your input.
-Patrick
Sunday, September 7, 2014 3:51 AM
Mr. Monkeyboy
I think what I am asking is in line with what the user agent is meant to do. To provide my web server with the details it needs to properly handle http requests from the App. To be clear I was not inquiring about how to change the user agent reported to any other website. I was looking for a way that my application installed locally could identify itself to the webserver when (for example) it accesses
http://example.com/image.png.
As I understand it, web servers make use of the user-agent string to have some information about the nature of the client making the http request. Isn't that what I am trying to accomplish?I'm not sure changing the registry is a good idea because it might change how the system reports itself to all websites, not just mine
I thank you for your input.
-Patrick
Well after extensive, time consuming research (about 15 minutes) I found that this function UrlMkSetSessionOption function can be used to programmatically set the User Agent string just for a process and does not affect anything outside of the process.
I'm not sure it will work for the non browser code you use though. But maybe it will.
Then I found a C# version of a Pinvoke function that performs this and used Teleriks online C# to Visual Basic converter to convert it to VB.Net.
Created a new app with a button and webbrowser control. Ran the app and navigated to http://whatsmyuseragent.com/. The user agent string shown was the user agent string I put in the code and is in the image below. Then I navigated again to that site and the string remained the same. So I navigated to that site with IE while my app was running too and IE's user agent string was the normal one it always shows at that site. So I suppose this code works fine maybe. I didn't test it alot.
Option Strict On
Imports System.Runtime.InteropServices
Public Class Form1
<DllImport("urlmon.dll", CharSet:=CharSet.Ansi)> _
Private Shared Function UrlMkSetSessionOption(dwOption As Integer, pBuffer As String, dwBufferLength As Integer, dwReserved As Integer) As Integer
End Function
Const URLMON_OPTION_USERAGENT As Integer = &H10000001
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.CenterToScreen()
ChangeUserAgent()
WebBrowser1.ScriptErrorsSuppressed = True
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
WebBrowser1.Navigate("http://whatsmyuseragent.com/")
End Sub
Public Shared Sub ChangeUserAgent()
Dim ua As String = "Hello. How are you today? I'm doing great!"
UrlMkSetSessionOption(URLMON_OPTION_USERAGENT, ua, ua.Length, 0)
End Sub
End Class
La vida loca
Sunday, September 7, 2014 4:30 AM
@Mr\. Monkeyboy
That's really interesting, I will try it out. I have found a way that works with pictureBox controls, but am still looking for a solution to AxShockwaveFlash control. What you suggested seems to affect application wide user agent; if that's the case, it is exactly what I need.
This is what I did to get PictureBox control to show the UserAgent of my choice when it queries an image online:
Dim pic As PictureBox = New PictureBox
'Manually draw the image from the result of an http request
pic.Image = Bitmap_From_URL("http://example.com/image.png")
Friend Function Bitmap_From_URL(ByVal Img_URL As String) As Bitmap
Dim rs As System.Net.HttpWebResponse
Dim bmp As System.Drawing.Bitmap 'return value
Dim strUserAgent As String = "AppName version X.X"
Dim hwrequest As Net.HttpWebRequest = Net.WebRequest.Create(Img_URL)
With hwrequest
.Accept = "image/*"
.AllowAutoRedirect = True
.UserAgent = strUserAgent '<-set User Agent here
.Method = "GET"
End With
'query the server and capture the response
rs = hwrequest.GetResponse()
'Create a bitmap from the response stream
bmp = New System.Drawing.Bitmap(rs.GetResponseStream())
rs.Close()
Return bmp
End Function
I am just now learning how to program, and have little to no experience with I/O operations so if you see something fundamentally wrong or dangerous here please let me know. This is only a partial solution however since it does not address the 2nd half of my problem: how to set useragent when I use AxShockwaveFlash object to show an swf (see my 1st post). I think your solution seems more promising.
-Patrick
Sunday, September 7, 2014 5:03 AM
I've never tried the code in your function. I have code that gets all the image URL's in a webpage and loads them into a ListBox. When a ListBox item (URL) is selected the image is downloaded and placed in a PictureBox using the below code. Of course instead of a PictureBox a Bitmap could be used.
The bolded, underlined code below would be where the URL string should go.
Dim Client As New WebClient()
Try
Dim PicStream As New MemoryStream(Client.DownloadData(CStr(ListBox1.Items.Item(ListBox1.SelectedIndex))))
PictureBox1.BackgroundImage = Image.FromStream(PicStream)
PicStream.Flush()
PicStream.Close()
Catch ex As Exception
End Try
Client.Dispose()
La vida loca
Sunday, September 7, 2014 5:32 AM
@Mr\. Monkeyboy
If all I cared about was putting the image at a URL into a picture box, I would just stick to what I currently do:
oPic.ImageLocation = "http://example.com/image.png"
Or if I wanted to use WebClient:
oPic.Image = New System.Drawing.Bitmap(New IO.MemoryStream(New System.Net.WebClient().DownloadData("http://example.com/image.png")))
The problem with both of those is that they don't allow me to set the User Agent. The code from my last post is something I wrote up today. It works but only helps with PictureBox controls, not AxShockwaveFlash controls. Your first solution still intrigues me the most because you seemed to think that it will set the User Agent for the whole process so that's exciting. I have to admit though that I don't understand what the line below does:
<DllImport("urlmon.dll", CharSet:=CharSet.Ansi)> _
Private Shared Function UrlMkSetSessionOption(dwOption As Integer, pBuffer As String, dwBufferLength As Integer, dwReserved As Integer) As Integer
End FunctionConst URLMON_OPTION_USERAGENT As Integer = &H10000001
I've never used that syntax. I'll try it though.
-Patrick
EDIT:
I have just tried your code Mr. Monkeyboy. Unfortunately it made no difference; maybe it is useful for setting the User Agent of the WebBrowser control, but it had no effect on the User Agent for
oPictureBox.ImageLocation="http://example.com/image.png"
Thanks for your advice.