我的守则如下: - 我不知道为什么我收到这条消息,请帮忙。马上 我正在使用沙盒帐户来测试这个。我已经生成了数据,即来自API资源管理器的示例数据,我将其作为参数传递给Json。
public bool GeneratePayment(string JsonData)
{
var principal = User as ClaimsPrincipal;
Session["realmId"] = "XXXXXX";
if (Session["realmId"] != null)
{
string realmId = Session["realmId"].ToString();
string qboBaseUrl = ConfigurationManager.AppSettings["QBOBaseUrl"];
//add qbobase url and query
string uri = string.Format("{0}/v3/company/{1}/invoice", qboBaseUrl, realmId);
try
{
var client = new HttpClient();
client.DefaultRequestHeaders.Add("Accept", "application/json;charset=UTF-8");
client.DefaultRequestHeaders.Add("ContentType", "application/json;charset=UTF-8");
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + "XXXX");
//Here i am getting waiting for activation
var result = client.PostAsync(uri, new StringContent(JsonData, System.Text.Encoding.UTF8, "application/json"));
return true;
}
catch (Exception ex)
{
return false;
}
}
else
return false;
}
答案 0 :(得分:0)
与Task
相关联的PostAsync
有关。
需要将GeneratePayment
方法设为异步,并且还需要等待client.PostAsync
public async Task<bool> GeneratePayment(string JsonData) {
var principal = User as ClaimsPrincipal;
Session["realmId"] = "XXXXXX";
if (Session["realmId"] != null) {
string realmId = Session["realmId"].ToString();
string qboBaseUrl = ConfigurationManager.AppSettings["QBOBaseUrl"];
//add qbobase url and query
string uri = string.Format("{0}/v3/company/{1}/invoice", qboBaseUrl, realmId);
try {
var client = http.Value; //singleton http client
var result = await client.PostAsync(uri, new StringContent(JsonData, System.Text.Encoding.UTF8, "application/json"));
return true;
} catch (Exception ex) {
return false;
}
}
else
return false;
}
//Singleton lazy loaded HttpClieny
static Lazy<HttpClient> http = new Lazy<HttpClient>(() => {
var client = new HttpClient();
client.DefaultRequestHeaders.Add("Accept", "application/json;charset=UTF-8");
client.DefaultRequestHeaders.Add("ContentType", "application/json;charset=UTF-8");
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + "XXXX");
return client;
});