[英]Invalid token ';' in class, struct, or interface member declaration
[英]Invalid Token '=' in class,struct or interface member declaration
因此,我尝试使用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;
哈比卜是正确的。 您需要将大部分代码放在一个方法中,但不能在类级别上全部包含。 在这里,我将其放入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
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.