HttpClient.PostAsync在Web API POST上返回Null

时间:2016-07-23 07:49:09

标签: post c#-4.0 asp.net-web-api deployment httpclient

我试图从几个GET和POST调用的API获得响应,每当我尝试从Web API POST获取响应时,它会提供 System.NullReferenceException:对象引用未设置为对象的实例即可。我正在使用PostAsync来获得响应。它在我的本地计算机上运行绝对正常,但在部署计算机上返回NULL响应

#include <iostream>//Input/Output Library
#include <cstdlib>
#include <iomanip>
using namespace std;//Namespace of the System Libraries

//Global Constants
const int MAX = 10;

//Function Prototypes
void input(int array[], int size);
void sort(int array[],int size);

int main(int argc, char** argv){
    //Declare Variable
    int array[MAX];
    int size = MAX;

    //Input Data
    input(array, size);
    sort(array, size);

    cout<<"\nSort   Frequency\n";
    for(int i = 0; i<size; i++){
        cout<<array[i]<<endl;
    }

    return 0;
}

void input(int a[], int size){
    cout<<"Enter "<<size<<" numbers for the array: \n";
    for(int i=0; i<size; i++){
        cin>>a[i];
    }
}
void sort(int a[],int n){
    for(int i=0;i<n-1;i++){
        for(int j=i+1;j<n;j++){
            if(a[i]>a[j]){
                a[i]=a[i]^a[j];
                a[j]=a[i]^a[j];
                a[i]=a[i]^a[j];
            }
        }
    }
}

对象:执行 #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 }; }

后消息为空

1 个答案:

答案 0 :(得分:-1)

尝试使用postasynch的范围内的字符串asynch进行读取,使用它所使用的相同代码块,

Properties prop = new Properties();
String filename = "C:\\workspaceNewI18NChnages\\customer\\Labels.properties";
FileInputStream in = new FileInputStream(filename);
props.load(in);

示例:

$(".gb-help-list ").on('mouseover', 'li', function (e) {
console.log('mouse');
var tl = new TimelineLite();
        tl.to($(event.delegateTarget).find('.line-help'), 2, {width: '100px', ease: Power4.easeOut})
        .to($(event.delegateTarget).find('.line-top'), 2, {width: '100%', ease: Power4.easeOut}, "-=1");
});

您还可以对任何您想要她的模型进行类型转换。