2010-12-14 2 views
1

다음을 시도했지만 http 헤더 (SOAP 요청)에 자격 증명을 추가하지 않았습니다.C#을 사용하여 webservice API 호출에서 자격 증명 정보를 전송 헤더로 추가하는 방법

MyWebService mySrv = new MyWebService(); 

System.Net.CredentialCache sysCredentail = new System.Net.CredentialCache();      
NetworkCredential netCred = new NetworkCredential("admin", "password"); 
sysCredentail.Add(new Uri(strSysURL), "Basic", netCred); 
mySrv.Credentials = sysCredentail; 

웹 서비스 API를 호출 할 때 자격 증명 정보를 추가 한 후 다음을 기대합니다.

SOAP::Transport::HTTP::Client::send_receive: POST http:/myurl/SYS HTTP /1.1 
Accept: text/xml 
Accept: multipart/* 
Accept: application/soap 
Content-Length: 431 
Content-Type: text/xml; charset=utf-8 
Authorization:: Basic YWRtaW46YnJvY2FkZQ== 
SOAPAction: "urn:webservicesapi#getSystemName" 

<?xml version="1.0" encoding="UTF-8"?> 

etc... 

하지만 인증 :: 기본 YWRtaW46YnJvY2FkZQ == 심지어 자격 증명을 추가 한 후이 없습니다. 친절히 조언합니다.

답변

0

다음 단계를 따르면이 문제가 해결되었습니다.

1) webserivce 클래스 (WSDL을 첨가 한 후 생성 된 자동 생성 된 클래스)

2) 두 함수 GetWebRequest 및 setRequestHeader를 무시로부터 유도 된 클래스를 만든다.

3) 대신 웹 서비스의 객체를 생성, 당신은 1 단계

4) setRequestHeader를를 사용하여 헤더 정보 (자격 정보) 설정에 의해 생성 된 클래스의 개체를 만듭니다.

5) 4 단계 후에 webservice API에 액세스하면 인증과 함께 작동합니다.

공용 클래스 MyDerivedService : MyService { private string m_HeaderName; 전용 문자열 m_HeaderValue;

//---------------- 
// GetWebRequest 
// 
// Called by the SOAP client class library 
//---------------- 
protected override WebRequest GetWebRequest(Uri uri) 
{ 
    // call the base class to get the underlying WebRequest object 
    HttpWebRequest req = (HttpWebRequest)base.GetWebRequest(uri); 

    if (null != this.m_HeaderName) 
    { 
     // set the header 
     req.Headers.Add(this.m_HeaderName, this.m_HeaderValue); 
    } 

    // return the WebRequest to the caller 
    return (WebRequest)req; 
} 

//---------------- 
// SetRequestHeader 
// 
// Sets the header name and value that GetWebRequest will add to the 
// underlying request used by the SOAP client when making the 
// we method call 
//---------------- 
public void SetRequestHeader(String headerName, String headerValue) 
{ 
    this.m_HeaderName = headerName; 
    this.m_HeaderValue = headerValue; 
} 

}

예 :

MyDerivedService objservice = new MyDerivedService(); 
string usernamePassword = username + ":" + password; 
objservice.SetRequestHeader("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(usernamePassword))); 
objservice.CallAnyAPI(); 
관련 문제