2013-09-23 94 views
5

我剛剛開始截取對我的WCF服務的請求。WCF AfterReceiveRequest獲取標題

我打電話使用Java代碼的Web服務,它看起來像這樣(短版)

connection = (HttpURLConnection)url.openConnection(); 
connection.setRequestMethod("GET"); 
connection.setRequestProperty("Username", "Testname"); 

我收到請求,但我不能讓/發現在消息請求的報頭。我試過這樣的事情:

public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext) 
{ 
    int headerIndex = request.Headers.FindHeader("Username", string.Empty); 
    var username = request.Headers["Username"] 

    return null; 
} 

但我總是以-1或例外結束。什麼是正確的方法來做到這一點?我在Java方面做錯了嗎?

回答

12

Message類中的Headers屬性將爲您提供SOAP標題;您正在尋找的是HTTP標題。要得到這些,你應該使用HttpRequestMessageProperty

public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext) 
    { 
     var prop = (HttpRequestMessageProperty)request.Properties[HttpRequestMessageProperty.Name]; 
     var userName = prop.Headers["Username"]; 

     return null; 
    } 
+0

謝謝!很難找到這樣的文件.. –