【问题标题】:Invalid Token '=' in class,struct or interface member declaration类、结构或接口成员声明中的无效标记“=”
【发布时间】:2014-09-29 22:23:14
【问题描述】:

所以我正在尝试使用 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 ')'
                }

我已将收到的错误包含在上面的代码中。希望他们能提高我遇到的问题的清晰度。

【问题讨论】:

  • 我相信您是在类级别直接编写此代码,看起来应该在方法内。
  • @Selman22 这是上面的内容:CredentialCache 缓存 = new CredentialCache(); CredentialCache cache.Add(Uri url); "基本",myCredentials);
  • 只要去掉 CredentialCache 这个词。为什么不使用我在上一个问题中已经给你的代码?这个问题已经为你解答了……
  • @Habib > 你能举个例子吗?我只用了一周的时间用 ASP/C# 进行编码。谢谢
  • 我已经使用了您提供的代码@eddie_cat,但仍然存在错误。因此,分解仍然无法工作的小块。

标签: c# asp.net


【解决方案1】:

你是说

request.Credentials = new CredentialCache(); 

代替

request.Credentials = CredentialCache cache; 

【讨论】:

    【解决方案2】:

    哈比卜是正确的。您需要将大部分代码放在一个方法中,您不能在类级别拥有所有代码。在这里,我将它放在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
                }
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2013-01-02
      • 1970-01-01
      • 1970-01-01
      • 2014-04-07
      • 2014-06-22
      • 2016-07-10
      • 1970-01-01
      • 2012-06-19
      • 2014-02-02
      相关资源
      最近更新 更多