无效令牌'=';在类、结构或接口成员声明中

本文关键字:接口 成员 声明 结构 在类 令牌 无效 | 更新日期: 2023-09-27 18:25:01

因此,我正在尝试使用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 ')'
                }

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

无效令牌'=';在类、结构或接口成员声明中

你是说吗

request.Credentials = new CredentialCache(); 

代替

request.Credentials = CredentialCache cache; 

Habib是正确的。您需要将这些代码的大部分放在一个方法中,不能将其全部放在类级别。在这里,我已经将它放在VebraController的构造函数中,但根据程序的执行流程,您可能希望以不同的方式执行它。我还删除了您为HttpHeader声明的内部类,因为我不认为您真的打算这么做。此代码中唯一剩下的编译错误在response.Write()行中。我不确定您想在那里做什么,因为HttpWebResponse不包括Write的方法定义。

请注意,您不需要为System.Net.Http等提供include语句。我包含的语句应该足够了。

我已经在方法之外声明了大多数变量——这是标准的,您可以将它们声明为类成员,这样您就可以在类中的任何地方使用它们。如果只在特定方法中需要它们,则可以在方法本身中声明它们。程序中的所有"操作"部分都需要采用一种方法。

   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
            }
        }
    }