2013-08-01 415 views
1

我給一個遠程REST service兩個調用一個接一個在單一的方法。我在第一次呼叫中設定值爲accessToken,並將其用於第二次請求。.ashx - 遠程服務器返回一個錯誤:(500)內部服務器錯誤

當我運行它,它給我的錯誤作爲

The remote server returned an error: (500) Internal Server Error.

以下是代碼。

HttpWebRequest webRequest = null; 
    HttpWebResponse webResponse = null; 
    Encoding encodingObj = null; 
    StreamReader streamReaderObj = null; 

    string grantCode = string.Empty; 
    string resultString = string.Empty; 
    string accessToken = string.Empty; 

    private void Instantiate() 
    {    
     grantCode = HttpContext.Current.Request.QueryString["code"].ToString(); 
     webRequest = (HttpWebRequest)WebRequest.Create(Constants.ACCESS_TOKEN_REQUEST + "&code=" + grantCode); 
     webRequest.Method = "GET"; 
     webRequest.ContentType = "application/json"; 
     webResponse = (HttpWebResponse)webRequest.GetResponse(); 
     encodingObj = System.Text.Encoding.GetEncoding("utf-8"); 
     streamReaderObj = new StreamReader(webResponse.GetResponseStream(), encodingObj); 
     resultString = streamReaderObj.ReadToEnd(); 
     JObject parameterCollection = JObject.Parse(resultString); 
     accessToken = parameterCollection["access_token"].ToString(); 
     //HttpContext.Current.Response.Write("<br/><br/>Code: <br/>" + grantCode); 
     //HttpContext.Current.Response.Write("<br/><br/>Access Token: <br/>" + accessToken); 

     webRequest = (HttpWebRequest)WebRequest.Create(Constants.RETRIEVE_CONTEXT_REQUEST + "vista-688/id/Staff01"); 
     webRequest.Method = "GET"; 
     webRequest.Accept = "application/json"; 
     webRequest.ContentType = "application/json"; 
     webRequest.Headers.Add("Authorization", "Bearer " + accessToken); 
     webResponse = (HttpWebResponse)webRequest.GetResponse(); 
     encodingObj = System.Text.Encoding.GetEncoding("utf-8"); 
     streamReaderObj = new StreamReader(webResponse.GetResponseStream(), encodingObj); 
     resultString = streamReaderObj.ReadToEnd(); 
     //HttpContext.Current.Response.Write("<br/><br/>Retrieve Context: <br/>" + resultString); 
    } 

這些是從配置文件得到充分的休息API網址:

<add key="GrantCodeRequest" value="https://<location>/AuthorizationServices/provider/authorize?response_type=code&state=mystateid&client_id=mVisum&redirect_uri=http://localhost:1316/RetrieveContext.aspx&scope=read"/> 
<add key="AccessTokenRequest" value="https://<location>/AuthorizationServices/oauth/token?client_id=mVisum&state=mystateid&scope=read&client_secret=TESTMVISUM&response_type=token&grant_type=authorization_code&redirect_uri=http://localhost:1316/RetrieveContext.aspx"/> 
<add key="RetrieveContextRequest" value="http://<location>/UserContext/rest/context/user/system/"/> 

當我做的只是用初始化爲有效值第二個呼叫accessToken值第二請求也被工作沒有任何異常。這個方法寫在一個處理程序中。

任何人都可以告訴我爲什麼會發生這種情況嗎? REST Web服務中沒有問題。我還通過使用兩個單獨的網絡請求和網絡響應對象,但沒有什麼工作了

+0

因此,*瀏覽器*顯示你500錯誤,但是你的代碼中發生了什麼異常?你是否嘗試過使用try/catch塊捕獲異常? –

+0

調試時rservice的完整url是什麼? –

+0

@Cory是的。它只是顯示相同的錯誤。沒有內在的例外。 – Ajinkya

回答

0

嘗試使用

 WebClient client = new WebClient(); 
     client.Headers["Content-type"] = @"application/json"; 
     Stream data = client.OpenRead(yoururl); ; 
     StreamReader reader = new StreamReader(data); 
     string responseFromServer = reader.ReadToEnd(); 

以上工作正常,我試過。

相關問題