使用 RestSharp、Xamarin 和 Visual studio 创建 GET 请求

Creating GET request using RestSharp, Xamarin and Visual studio

我正在尝试为 android 和 Visual Studio 2015 在 Xamarin 中的服务器创建一个 RestSharp GET 请求。

我使用 Fiddler 查看来自 Chrome 浏览器的请求,就是这样:

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

我尽力使用 RestSharp 提出了类似的请求并得到了这个:

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);

请求没有崩溃,但是当我在快速查看中查看 queryResult 并收到此错误消息时:"Unexpected declaration markup was found. Line 2, position 3."

这是内容:

\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

我做错了什么? 有人可以帮帮我吗?

提前致谢!

您正在这样做:

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

在这一行中,您告诉 client 您希望从网络服务器接收 JSON 字符串。因此 RestSharp 尝试将传入响应视为 JSON 字符串。 问题是您没有收到 JSON。您收到 HTML。所以 JSON 解析器抛出异常,因为它无法处理 HTML.

你最期待什么样的回应?

您对 HTML 的回复满意吗?你想让它在任何地方存储内容字符串吗?然后这样做:

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

如果您希望收到 JSON 字符串,则可以调用 client.Execute<JsonObject>(request)。但是你应该看看网络服务的文档。你好像用错了