简体   繁体   中英

Invalid Token '=' in class,struct or interface member declaration

So I'm trying to use the request.Credentials function and am having the following error after building the solution..

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

I have included the errors received within the code above. Hopefully they will improve clarity of the issues I have.

Do you mean

request.Credentials = new CredentialCache(); 

Instead of

request.Credentials = CredentialCache cache; 

Habib is correct. You need to put most of this code inside a method, you cannot have it all at the class level. Here I've put it inside the constructor for VebraController , but depending on the flow of execution of your program you may wish to do it differently. I also removed the inner class you were declaring for HttpHeader as I don't think you really meant to do that. The only remaining compilation error with this code is in the response.Write() line. I am not sure what you are trying to do there as HttpWebResponse does not include a method definition for Write .

Note that you don't need to have include statements for System.Net.Http etc. The ones I have included should be sufficient.

I have declared most of the variables outside the method--this is standard, you declare them as class members so you can use them anywhere in the class. If you only need them in a particular method you can declare them within the method itself. All the "action" parts of your program need to go in a method.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM