【发布时间】:2014-09-29 22:23:14
【问题描述】:
所以我正在尝试使用 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 ')'
}
我已将收到的错误包含在上面的代码中。希望他们能提高我遇到的问题的清晰度。
【问题讨论】:
-
我相信您是在类级别直接编写此代码,看起来应该在方法内。
-
@Selman22 这是上面的内容:CredentialCache 缓存 = new CredentialCache(); CredentialCache cache.Add(Uri url); "基本",myCredentials);
-
只要去掉 CredentialCache 这个词。为什么不使用我在上一个问题中已经给你的代码?这个问题已经为你解答了……
-
@Habib > 你能举个例子吗?我只用了一周的时间用 ASP/C# 进行编码。谢谢
-
我已经使用了您提供的代码@eddie_cat,但仍然存在错误。因此,分解仍然无法工作的小块。