그래서 기본적으로 외부에서 호스팅되는 소스에서 내 프로젝트에 웹 API를 통합하는 작업을하고 있습니다.하지만 xml은 기본 인증 뒤에 저장됩니다. 그래서 XML의 위치에 접속할 때 HTTP 요청의 헤더로 인증을 구문 분석해야한다는 조언을 받았습니다.
현재 작업중인 작업은 다음과 같습니다.
컨트롤러를 만들었는데 코드는 다음과 같습니다.
namespace com.tortoise.Controllers
{
public class VebraController : ApiController
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("url");
string username = "user";
string password = "password";
string usernamePassword = ("username + : + password");
CredentialCache cache = new CredentialCache();
new cache.add Uri(url), "Basic", new class NetworkCredential(username, password));
request.Credentials = cache;
request.Headers.Add("Authorization", "Basic " // <- space here.
+ Convert.ToBase64String()(new Int64 ASCIIEncoding().GetBytes (usernamePassword));
// Get the token from the response:
string token = response.GetResponseHeader("Token");
}
어떤 도움이라도 좋습니다. CredentialCache, ASCIIEncoding, ToBase64String (), GetBytes () 및 GetResponseHeader ()에서 오류가 발생합니다.
알겠습니다. 코드를 약간 수정했습니다. 사용중인 변수를 실제로 선언했다고 가정하고 지금 컴파일해야합니다. 나는 당신을 위해 그것을 할 수 없습니다. 댓글을 읽어주세요. C # 구문과 일반적으로 언어에 대한 많은 연습이 필요한 것 같습니다. 거기에 잘못된 C #이 많이 있습니다. 도움이 되었기를 바랍니다.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
string username = "user";
string password = "password";
//here I am declaring the NetworkCredentials. You do not need to put 'new class'
NetworkCredential myCredentials = new System.Net.NetworkCredential(username,password);
string usernamePassword = (username + password); //I assume you meant to concatenate them
CredentialCache cache = new CredentialCache();
cache.Add((Uri)url, "Basic", myCredentials); //you must declare url, not sure what you want it to be
request.Credentials = cache;
request.Headers.Add("Authorization", "Basic " // <- space here.
+ Convert.ToBase64String(Encoding.ASCII.GetBytes(usernamePassword)); //fixed this
// Get the token from the response:
string token = response.GetResponseHeader("Token"); //you need to declare response somewhere
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다