类,结构或接口成员声明中的标记'='无效

时间:2014-09-29 13:38:43

标签: c# asp.net

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

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

2 个答案:

答案 0 :(得分:3)

你的意思是

request.Credentials = new CredentialCache(); 

而不是

request.Credentials = CredentialCache cache; 

答案 1 :(得分:0)

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