C# 无效令牌'=';在类、结构或接口成员声明中
因此,我尝试使用C# 无效令牌'=';在类、结构或接口成员声明中,c#,asp.net,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 { publi
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;
你是说
request.Credentials = new CredentialCache();
而不是
request.Credentials = CredentialCache cache;
你是说
request.Credentials = new CredentialCache();
而不是
request.Credentials = CredentialCache cache;
你是说
request.Credentials = new CredentialCache();
而不是
request.Credentials = CredentialCache cache;
哈比卜是正确的。您需要将这些代码的大部分放在一个方法中,而不能将它们全部放在类级别。在这里,我将它放在
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
}
}
}
哈比卜是正确的。您需要将这些代码的大部分放在一个方法中,而不能将它们全部放在类级别。在这里,我将它放在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
}
}
}
哈比卜是正确的。您需要将这些代码的大部分放在一个方法中,而不能将它们全部放在类级别。在这里,我将它放在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
}
}
}
哈比卜是正确的。您需要将这些代码的大部分放在一个方法中,而不能将它们全部放在类级别。在这里,我将它放在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
}
}
}
我相信您是在类级别直接编写这段代码,正如它所显示的,它应该在一个方法中。@Selman22这就是上面的内容:CredentialCache cache=new CredentialCache();CredentialCache cache.Add(Uri url);“基本”,我的证书);只需删除CredentialCache这个词。为什么不使用我在上一个问题中给你的代码呢?这个问题已经为你解答了…@Habib>你能举个例子吗?我刚刚开始用ASP/C编写代码一周。ThanksI使用了您提供的@eddie_cat代码,但仍然存在错误。因此,分解仍然不起作用的较小的块。我相信您是在类级别直接编写此代码,正如它所显示的,它应该在一个方法中。@Selman22这就是上面的内容:CredentialCache cache=new CredentialCache();CredentialCache cache.Add(Uri url);“基本”,我的证书);只需删除CredentialCache这个词。为什么不使用我在上一个问题中给你的代码呢?这个问题已经为你解答了…@Habib>你能举个例子吗?我刚刚开始用ASP/C编写代码一周。谢谢,我使用了您提供的@eddie_cat代码,但仍然有错误