![](/img/trans.png)
[英]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.