2016-07-23 137 views
0

我想在幾次GET和POST調用時從API獲取響應,每當我嘗試從Web API POST獲取響應時,都會給出一個System.NullReferenceException:對象引用未設置爲對象的實例。。我正在使用PostAsync來獲取響應。它在我的本地機器上絕對正常,但在部署機器上返回NULL響應。在Web API上返回Null的HttpClient.PostAsync POST

    #region API_CALL 

        System.Net.Http.HttpClient Client = new System.Net.Http.HttpClient(); 
        string CompleteURL = URL.Trim() + URL_FromJSON.Trim(); 

        #region URL Construct For GET Requests 

        CompleteURL = GetURL(ParameterValue, CallType_FromJSON, CompleteURL); 

        #endregion URL Construct For GET Requests 

        string URLContent = string.Empty; 
        Client.BaseAddress = new System.Uri(CompleteURL); 
        byte[] cred = UTF8Encoding.UTF8.GetBytes(ClientID.Trim() + Constants.APIConstants.ColonConnector + ClientPass.Trim()); 
        string Encoded = Convert.ToBase64String(cred); 
        Client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(Constants.APIConstants.AuthTypeBearer, accessToken); 
        Client.DefaultRequestHeaders.Host = IPAddress.Trim(); 
        CacheControlHeaderValue val = new CacheControlHeaderValue(); 
        val.NoCache = true; 
        Client.DefaultRequestHeaders.CacheControl = val; 
        Client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue(Constants.APIConstants.ContentType)); 
        Client.DefaultRequestHeaders.Add(Constants.APIConstants.LicenseKey, LiscenseKEY); 
        Client.DefaultRequestHeaders.Add(Constants.APIConstants.PostmanToken, PostmanToken); 
        //client.DefaultRequestHeaders.Add(Constants.APIConstants.AccessToken, accessToken); 

        System.Net.Http.HttpContent Content = new StringContent(JSONData, UTF8Encoding.UTF8, Constants.APIConstants.ContentType); 

        // Only for debug purpos: 
        LogManager.Logger.Invoke(LogLevels.TraceDetail, Source, "Request to API: " + JSONData, Request.ActorID); 


        HttpResponseMessage Message = new HttpResponseMessage(); 

        if (CallType_FromJSON == Constants.HttpMethod.Post) 
        { Message = Client.PostAsync(CompleteURL, Content).Result; } 
        else if (CallType_FromJSON == Constants.HttpMethod.Get) 
        { Message = Client.GetAsync(CompleteURL, HttpCompletionOption.ResponseContentRead).Result; } 
        string Description = string.Empty; 

        #region Response Logging 

        try 
        { 
         LogManager.Logger.Invoke(LogLevels.TraceDetail, Source, "Response from API: " + Message.Content.ReadAsStringAsync().Result, Request.ActorID); 
        } 
        catch(Exception EX) 
        { 
         LogManager.Logger.Invoke(LogLevels.TraceError, Source, "Exception while logging response from API" + EX.ToString()+"Stack: "+EX.StackTrace, Request.ActorID); 
        } 

        #endregion Response Logging 

        if (Message.IsSuccessStatusCode) 
        { 
         string Result = Message.Content.ReadAsStringAsync().Result; 
         ResponseJson = Result; 
         Description = Result; 
        } 
        else 
        { 
         try 
         { 
          string Result1 = Message.Content.ReadAsStringAsync().Result; 
          LogManager.Logger.Invoke(LogLevels.TraceInfo, Source, "Failed: ... Recieved JSON: " + Result1, Request.ActorID); 
         } 
         catch { } 
         return new TransactionResponse() { Response = Constants.ResponseCodes.Error }; 
        } 
        #endregion API_CALL 

        break; 
      } 

      TransactionResponse Response = new TransactionResponse(); 
      Response.Response = ResponseJson; 
      return Response; 
     } 
     catch (Exception Ex) 
     { 
      LogManager.ExpLogger.Invoke(Source, Ex, Request.ActorID); 
      return new TransactionResponse() { Response = Constants.ResponseCodes.Error }; 
     } 

對象:消息的Message = Client.PostAsync(CompleteURL, Content).Result

+0

你能顯示你的api代碼並提供一些細節嗎?很難理解你的代碼和問題。 – afrikaan

+0

@kyur我添加了代碼... –

+0

您是否嘗試過使用Fiddler來檢查發送的實際HTTP請求是什麼? – Kenta

回答

0

嘗試閱讀作爲字符串非同步與在postasynch的範圍,隨着它的工作原理同一代碼塊用它執行後爲空,

if (CallType_FromJSON == Constants.HttpMethod.Post) 
{ Message = Client.PostAsync(CompleteURL, Content).Result; } 
else if (CallType_FromJSON == Constants.HttpMethod.Get) 
{ Message = Client.GetAsync(CompleteURL, HttpCompletionOption.ResponseContentRead).Result; } 
string Description = string.Empty; 

例如:

var = httpresult.content.readasstringasyhnc(); 

您也可以強制轉換爲你希望她任何模型。