问题
I know there are a lot of topics containing the same subject as this one, but non of those topic could help me with my problem.
I'm making a simple iPhone Webbrowser in C#. The only thing it should do, is acting like it's an iPhone. This is possible when you change the User Agent String.
I tried in in the WebBrowser.Navigate event, in the 4th argument you can put a header containing a User Agent String. But it's working just one time, after you click something in the WebBrowser it changes the user string to default. So this is not a useable method for me.
Then i tried an ExtendedWebBrowser control that i found on the internet. When you youse that control you can set the User Agent in the properties windows, but this doesn't work at all for me.
So at last i tried to use the urlmon.dll to set the User Agent, but everywere when they talk about it, they talk about restarting the process. I can't find any process working with that DLL so i can't get is to work either. And if it would work, it's not a possibility to restart it everytime i want to use the application.
Can someone help me to get a answer to my question, i'm struggling with for a day now?
Thank you in advance people!
回答1:
Here's how you change the UserAgent
[DllImport("urlmon.dll", CharSet = CharSet.Ansi)]
private static extern int UrlMkSetSessionOption(int dwOption,
string pBuffer, int dwBufferLength, int dwReserved);
const int URLMON_OPTION_USERAGENT = 0x10000001;
string ua = "YourUserAgentString";
UrlMkSetSessionOption(URLMON_OPTION_USERAGENT, ua, ua.Length, 0);
回答2:
I don't have anything to add for this specific issue, but it should be mentioned that IE is not going to render things anywhere close to how an iPhone will, especially for sites that include iPhone-specific markup, scripts, and stylesheets.
Google Chrome is WebKit-based (the same as the iPhone browser) and there's an option in the Developer Tools to override the User Agent. It may be a better solution than trying to host IE under C#. Chrome's debugging tools are also a lot nicer for poking around in the DOM.
来源:https://stackoverflow.com/questions/9479329/change-user-agent-webbrowser-object-c-sharp