I am using C# to call the PRTG API. I get past authorization, but can't get past a "browser not supported" error message. I have tried a few settings, but haven't had any luck getting past it.
Any suggestions?
Votes:
I am using C# to call the PRTG API. I get past authorization, but can't get past a "browser not supported" error message. I have tried a few settings, but haven't had any luck getting past it.
Any suggestions?
13 Replies
Votes:
Hi,
You can use a HttpWebRequest and set the UserAgent property.
Code snippet in VB .NET
Dim request As HttpWebRequest = CType(HttpWebRequest.Create(_url), HttpWebRequest) With request .Timeout = 5000 .Method = "GET" .ContentType = "text/xml" .Headers.Add(HttpRequestHeader.CacheControl, "max-age=0") .UserAgent = "Mozilla 4.0" .CachePolicy = noCachePolicy End With
Votes:
Thanks for the quick reply!
I went down a similar path without much luck. Here is the code I am using (but I am a C# guy):
HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest; req.Timeout = 5000; req.Method = "GET"; req.ContentType = "text/xml"; req.Headers.Add(HttpRequestHeader.CacheControl, "max-age=0"); req.UserAgent = "Mozilla 4.0"; var noCachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore); req.CachePolicy = noCachePolicy; string result = null; using (HttpWebResponse resp = req.GetResponse() as HttpWebResponse) { var reader = new StreamReader(resp.GetResponseStream()); result = reader.ReadToEnd(); }
The response I get does not even come back as XML. It looks like:
<!doctype html>\n<html class=\"\">\n<head>\n <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge,chrome=1\">\n <meta charset=\"utf-8\">\n <meta name=\"viewport\" content=\"width=device-width,initial-scale=1\">\n <meta name='viewport' content='width=device-width, height=device-height, initial-scale=0.8'>\n <link id=\"prtgfavicon\" rel=\"shortcut icon\" type=\"image/ico\" href=\"/favicon.ico\" />\n <title>Welcome | DWS Network Monitoring System</title>\n <link rel=\"stylesheet\" type=\"text/css\" href=\"/css/prtgmini.css?prtgversion=14.1.8.1371+__\" media=\"print,screen,projection\" />\n\n \n \n\n \n \n \n</head>\n<body id=\"mainbody\" class=\"systemmenu loginscreen language_en\" onload='onLoad();'>\n\n\r\n\r\n<div id=\"login-container\">\r\n\t<div class=\"header\">\r\n\t\t<span class=\"prtglogo\">\r\n\t\t\t<img border=0 id=\"prtgname\" src=\"/images/prtg_network_monitor.png\">\r\n\t\t</span>\r\n\t\t<span class=\"paesslerlogo\">\r\n\t\t\t<a href=\"https://www.paessler.com\" target=\"_blank\" tit le=\"Paessler AG - The Network Monitoring Company\"><img border=0 id=\"paesslerlogo\" src=\"/images/paessler.png\"></a>\r\n\t\t</span>\r\n\t</div>\r\n\r\n\t<div class=\"login-form\" style=\"\">\r\n\t\t<div class=\"login-cell\">\r\n\t\t\t<table>\r\n\t\t\t\t<tr>\r\n\t\t\t\t<td>\r\n\t\t\r\n\t\t\t\t\t<h1>DWS Network Monitoring System</h1>\r\n\r\n<noscript>\r\n <div style=\"margin-bottom:10px\">\r\n <div class=\"nagscreen-box\" >\r\n <p class=\"nagscreen-head\">\r\n Javascript not available!\r\n </p>\r\n <p class=\"nagscreen-cell\">\r\n You cannot use the AJAX Web Interface without Javascript. <br>It seems to be disabled or this browser does not support Javascript.\r\n </p>\r\n </div>\r\n </div>\r\n</noscript>\r\n\t\t\t\t\t<div id=\"unsupportedbrowser\" style=\"margin-bottom:10px;display:none;\">\r\n\t\t\t\t\t\t<div class=\"nagscreen-box\" >\r\n\t\t\t\t\t\t <p class=\"nagscreen-head \">\r\n\t\t\t\t\t\t Sorry, your browser is not supported!\r\n\t\t\t\t\t\t </p> ETC...
This doesn't seem right? Calling environment is VS2013 console application on Windows 8.1.
I can utilize the same URL in PostMan (REST service utility) within Chrome and get the proper results back. I don't understand what the difference between calls in PostMan and the calls from VS.
Any help is appreciated. THANKS!
Created on Apr 15, 2014 2:48:45 PM
Last change on Apr 15, 2014 2:54:37 PM by
Konstantin Wolff [Paessler Support]
Votes:
You can try this:
Dim doc As New XmlDocument Using response As WebResponse = request.GetResponse() Dim mem As New MemoryStream() Dim s As Stream = response.GetResponseStream() '// load response in memory stream, to determine decoding While (True) Dim i As Integer = s.ReadByte() If i < 0 Then Exit While mem.WriteByte(CByte(i)) End While mem.Flush() mem.Position = 0 Dim xtReader As New XmlTextReader(mem) xtReader.MoveToContent() '// determine decoding Dim enc As System.Text.Encoding Try enc = xtReader.Encoding Catch ex As Exception enc = System.Text.Encoding.GetEncoding("ISO8859-1") End Try mem.Position = 0 '// read memory stream again, now using the found encoding Dim reader As New StreamReader(mem, enc) '// load xmldoc from streamreader Dim lolcatXml As String = reader.ReadToEnd() doc.LoadXml(lolcatXml) xtReader.Close() End Using
Votes:
I have gotten back to this a couple times and spent many, many hours on it. I still cannot get the API to return anything other than 'browser not supported'. Maybe its the URL? With the reading code above, I get a docType error as the response is HTML and not XML. There is nothing of value to display in the response. A sample URL I am using is:
https://myProvider.com/api/table.xml?content=sensortree&output=xml&username=UName&passhash=Pwd
i have tried several URLs from the documentation and haven't been able to get any to work. Any help or ideas would be appreciated.
Have a great day!
Votes:
I did a bit more digging into the response and header info from the browser. This isn't really a web service, but a file download that is closer to FTP than REST. I am not sure how Postman renders the xml data via call and attachment access, but .NET code doesn't seem to be able to get it. I can download the file from any browser without any problem. The input of the URL prompts for a standard file download. I think this is the expected behavior and its fine. When calling from code, it appears to do an odd redirect to some type of home page. There is not any attachment on the response to be found.
The download from the browser is the expected xml. The download from the .NET app is still the unexpected HTML.
HELP!!!!!
Votes:
Have you tried using the
WebClient.DownloadFile Method?
http://msdn.microsoft.com/en-us/library/ez801hhe(v=vs.110).aspx
Votes:
Sure did. I do get a file, but it is the same raw HTML of 'browser not supported' with a bunch of links to other news posts like I would get on a redirect page. I did add all the user agent settings too and it didn't make a difference.
There is nothing else in the message to parse.
Sorry.
Votes:
Sure did, but only got the raw HTML as a file. I also tried adding user agent settings too.
Votes:
If your PRTG Server is accessible form the internet and you are willing to set up a temporarily (read-only) account, I'll be glad to have a look.
You can find my email address at http://www.prtgtoolsfamily.com?page=contact
Votes:
I appreciate the offer. I am not sure I can assign rights to you as I don't manage the platform. Its a shared instance via our data center provider.
I have tried it a different way and it appears that the request is actually auto redirecting me to the default login page since that is the response uri. There is some implication that I am not authenticating correctly upon submission.
THANKS for the help!
Votes:
Thanks for the help and assistance. I finally got it to work. The problem with with authentication and the password hash not being accepted. The authentication must have passed since I didn't get 'unauthorized', but the authorization did to the resource didn't pass which caused a redirect to the login page.
The service thought it was working hence the 'okay' response from the API.
Have a great night!
Votes:
That's good news!
Please remember to always URL-Encode the username and password in your requests.
For example:
https://myprtgserver/api/(...)&username=@dmin&password=!@#
Needs to be encoded to:
https://myprtgserver/api/(...)&username=%40dmin&password=!%40%23
Votes:
I recently started looking into creating c# version of some powershell scripts and would need to store the XML data from the API pages into a variable. Did some searching and found this.
https://docs.microsoft.com/en-us/troubleshoot/dotnet/csharp/read-xml-data-from-url
©2024 Paessler AG Terms & Conditions Privacy Policy Legal Notice Download & Install
Add comment