2015-11-04 104 views
1

我試圖創建一個RestSharp GET請求到服務器Xamarin的Android和Visual Studio 2015年創建使用RestSharp,Xamarin和Visual Studio

我用小提琴手看到從Chrome瀏覽器的請求GET請求這就是它:

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

請求不會崩潰,但是當我看進queryResu在快速觀察中得到了這個錯誤信息:「發現了意外的聲明標記。 2號線,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 

我在做什麼錯 可有人請給我一個手

預先感謝您

回答

0
?!

您正在做的是:

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

在這一行你告訴client您希望從Web服務器接收JSON字符串。因此RestSharp會嘗試將傳入的響應視爲JSON字符串。 問題是你沒有收到JSON。您收到HTML。所以JSON解析器拋出一個異常,因爲它無法處理HTML。

你真的期待什麼樣的反應?

你對HTML響應滿意嗎?你想要它將內容字符串存儲在任何地方嗎?那麼做到這一點:

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

如果您希望收到一個JSON字符串,然後調用client.Execute<JsonObject>(request)是OK。但你應該看看web服務的文檔。你似乎錯誤地使用它。

相關問題