2014-09-29 87 views
0

所以我想用request.Credentials功能和構建解決方案後,我有以下錯誤..無效令牌「=」在類,結構或接口成員聲明

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net; 
using System.Net.Http; 
using System.Web.Http; 
using System.Web.Caching; 


namespace com.tortoise.Controllers 
{ 
    public class VebraController : ApiController 
    { 

     public class HttpHeader 
     { 
     string username = "foo"; 
     string password = "foo"; 

     string url = "www.test.com"; 

     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 

     . 
     NetworkCredential myCredentials = new System.Net.NetworkCredential(username,password); 
     string usernamePassword = (username + password); 

     cache = new CredentialCache(); 
//Invalid Token '=' in class,struct,interface member declaration, also for CredentialCache > //Method must have a return type. 

      CredentialCache cache.Add(Uri url); "Basic",myCredentials); 
//Invalid token "Basic" in class,struct,or interface member declaration, same with the ')'. 

      request.Credentials = CredentialCache cache; 
//Invalid Token '=' in class,struct,interface member declaration 

      request.Headers.Add("Authorization", "Basic " + 
//Invalid Token '(' in class,struct,interface or declaration 
Convert.ToBase64String(Encoding.ASCII.GetBytes(usernamePassword)); 
//Invalid Token '(' in class,struct,interface or declaration same for GetBytes. and end of usernamePassword 

      // Get the token from the response: 
      string token = response.GetResponseHeader("Token"); 


      HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
      response.Write (response.StatusCode) ; 
//Invalid Token '(' in class,struct,interface or declaration same for ')' 
       } 

我已經包括錯誤在上面的代碼中收到。希望他們能夠提高我擁有的問題的清晰度。

+6

我相信你是直接在類的級別編寫代碼,因爲它看起來應該在一個方法內。 – Habib 2014-09-29 13:39:33

+0

@ Selman22 這就是以上內容: CredentialCache cache = new CredentialCache(); CredentialCache cache.Add(Uri url); 「基本」,myCredentials); – SavageDragon87 2014-09-29 13:42:19

+1

只需刪除CredentialCache一詞。爲什麼不使用我在之前的問題中已經給你的代碼?這個問題已經爲你解答了...... – 2014-09-29 13:43:11

回答

0

哈比卜是正確的。你需要將這些代碼的大部分放在一個方法中,你不能在課堂上使用它。這裏我已經把它放在VebraController的構造函數中,但是根據程序的執行流程,你可能希望做不同的處理。我也刪除了你爲HttpHeader聲明的內部類,因爲我不認爲你真的打算這麼做。該代碼中唯一剩下的編譯錯誤在response.Write()行中。我不確定你在那裏做什麼,因爲HttpWebResponse不包括Write的方法定義。

請注意,您不需要包含System.Net.Http等聲明。我所包含的應該是足夠的。

我已經聲明瞭方法外的大部分變量 - 這是標準的,您將它們聲明爲類成員,以便您可以在類中的任何位置使用它們。如果你只需要特定的方法,你可以在方法本身中聲明它們。程序中的所有「動作」部分都需要採用某種方法。

using System; 
    using System.Net; 
    using System.Web; 
    using System.Text; 

    namespace com.tortoise.Controllers 
    { 
     public class VebraController : ApiController 
     { 
       private string username = "foo"; //class member 
       private string password = "foo"; //class member 

       private static string url = "www.test.com"; //class member 

       //this is where the constructor starts 
       public VebraController() { 

       HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 

       NetworkCredential myCredentials = new System.Net.NetworkCredential(username,password); 
       string usernamePassword = (username + password); 

       CredentialCache cache = new CredentialCache(); 

       cache.Add(new Uri(url), "Basic", myCredentials); 

       request.Credentials = cache; 

       request.Headers.Add("Authorization", "Basic " + 
        Convert.ToBase64String(Encoding.ASCII.GetBytes(usernamePassword)); 

       HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
       // Get the token from the response: 
       string token = response.GetResponseHeader("Token"); 

       response.Write(response.StatusCode); //you need to fix this 
      } 
     } 
    } 
3

你的意思

request.Credentials = new CredentialCache(); 

而不是

request.Credentials = CredentialCache cache; 
相關問題