简体   繁体   中英

Creating GET request using RestSharp, Xamarin and Visual studio

I'm trying to create a RestSharp GET request to a server in Xamarin for android and Visual Studio 2015.

I used Fiddler to see the request from the Chrome browser and this is it:

GET http://10.10.20.72:8090/ProcessTOGO/Login.aspx HTTP/1.1
Host: 10.10.20.72:8090
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36
Referer: http://10.10.20.72:8090/ProcessTOGO/
Accept-Encoding: gzip, deflate, sdch
Accept-Language: he-IL,he;q=0.8,en-US;q=0.6,en;q=0.4
Cookie: ASP.NET_SessionId=iirbfr55caklfmnebmfqxmjr; i18next=en-US

I tried my best to make a similar request using RestSharp and got this:

CookieContainer cookies = new CookieContainer();

cookies.Add(new System.Uri(@"http://10.10.20.72:8090/ProcessToGo/Login.aspx"), new Cookie("ASP.NET_SessionId", "iirbfr55caklfmnebmfqxmjr"));
cookies.Add(new System.Uri(@"http://10.10.20.72:8090/ProcessToGo/Login.aspx"), new Cookie("i18next", "en-US"));
var client = new RestClient(@"http://10.10.20.72:8090/ProcessToGo/Login.aspx");
var request = new RestRequest(Method.GET);

client.UserAgent = @"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36";
client.CookieContainer = cookies;

var queryResult = client.Execute<JsonObject>(request);

The request does not crash but when I look into the queryResult in the quick watch and got this error message: "Unexpected declaration markup was found. Line 2, position 3."

This is the content:

\r\n
<!doctype html>\r\n
<html>\r\n
<head>
    <title>\r\n\t
        Process To Go > Login\r\n
    </title>
    <meta charset=\"utf-8\" />
    <meta name=\"viewport\" content=\"width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;\" />
    <meta name=\"apple-mobile-web-app-capable\" content=\"yes\" />
    <meta name=\"apple-mobile-web-app-status-bar-style\" content=\"black\" />
    <meta name=\"HandheldFriendly\" content=\"True\" />
    <meta name=\"MobileOptimized\" content=\"320\" />\r\n\t
        <!-- Mobile IE allows us to activate ClearType technology for smoothing fonts for easy reading -->\r\n\t
    <meta http-equiv=\"cleartype\" content=\"on\" />\r\n\t
        <!-- Custom css -->\r\n\t
    <link id=\"cssFile\" type=\"text/css\" rel=\"stylesheet\" href=\"css/custom.css\" media=\"screen\" />
</head>\r\n
<body style=\"background:#35749d\">\r\n    
    <form name=\"form1\" method=\"post\" action=\"Login.aspx\" id=\"form1\">\r\n
        <div>\r\n
            <input type=\"hidden\" name=\"__VIEWSTATE\" id=\"__VIEWSTATE\" value=\"/wEPDwULLTEzMTYyMTY0NjFkZOXMM9hn0xf/xrGoXm3/cuf05qnm\" />\r\n
        </div>\r\n\r\n
        <div>\r\n\r\n\t
            <input type=\"hidden\" name=\"__VIEWSTATEGENERATOR\" id=\"__VIEWSTATEGENERATOR\" value=\"1F881200\" />\r\n
        </div>\r\n 
        <div id=\"loginPage\">\r\n
            <div id=\"logo\"></div>\r\n
            <ul class=\"errors\">\r\n
                <li id=\"errUsername\" data-i18n=\"login.errUsername\"></li>\r\n
                <li id=\"errPassword\" data-i18n=\"login.errPassword\"></li>\r\n
                <li id=\"errServer\" data-i18n=\"login.errServer\"></li>\r\n 
            </ul>\r\n
            <input name=\"txtUsername\" type=\"text\" id=\"txtUsername\" placeholder=\"User Name\" class=\"txtbox\" data-i18n=\"[placeholder]login.userName\" autocomplete=\"off\" />\r\n       
            <input name=\"txtPassword\" type=\"password\" id=\"txtPassword\" placeholder=\"Password\" class=\"txtbox\" data-i18n=\"[placeholder]login.password\" autocomplete=\"off\" />\r\n       
            <input type=\"submit\" name=\"btnLogin\" value=\"Sign-in\" id=\"btnLogin\" class=\"btnLogin\" />\r\n   
        </div>\r\n    
    </form>\r\n \t\r\n    
    <script src=\"http:////ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js\"></script>\r\n    
    <script>window.jQuery || document.write('<script src=\"js/libs/jquery-1.8.1.min.js\"><\\/script>')</script>\r\n    
    <script src=\"js/libs/i18next-1.6.0.min.js\" type=\"text/javascript\"></script>\r\n
    <script src=\"js/libs/jquery.cookie.js\" type=\"text/javascript\"></script>\r\n  
    <script src=\"js/login.js\" type=\"text/javascript\"></script>\r\n
</body>\r\n
</html>\r\n

What am I doing wrong? Can someone please give me a hand?

Thank you in advance!

You are doing this:

var queryResult = client.Execute<JsonObject>(request);

In this line you tell the client that you expect to receive JSON string from the webserver. So RestSharp tries to treat the incoming response as a JSON string. The problem is that you don't receive JSON. You receive HTML. So JSON parser throws an exception since it can't handle HTML.

Which kind of response do you really expect?

Are you happy with the HTML response? Do you want it to store the content string anywhere? Then do this:

var queryResult = client.Execute<JsonObject>(request);     
var htmlContent = queryResult.Content;

In case you expect to receive a JSON string then the call to client.Execute<JsonObject>(request) is OK. But you should take a look into the documentation of the webservice. You seem to use it wrong.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM