我正在尝试将JSON发布到Qualtrics API以发送调查请求。
当所有必要的ID都有效并且我收到如下响应时,这可以正常工作。
{"result":{"id":"EMD_0xpOmBl99999999"},"meta":{"httpStatus":"200 - OK","requestId":"a075f677-bbcf-45fb-bb7d-328999999999"}}
如果我将其中一个值更改为无效ID,则API应返回“400 Bad request”错误,并显示一条消息,告诉您哪个值不正确,例如
{"meta":{"httpStatus":"400 - Bad Request","error":{"errorMessage":"Invalid surveyId parameter. surveyLink must contain a valid survey","errorCode":"JP_2"},"requestId":null}}
但是,在我的C#应用程序中,我没有收到预期的400错误,代码只是无限期挂起在WebClient.UploadString步骤上。
如果使用Poster等测试工具发布相同的JSON,我会得到预期的响应。
我该如何解决这个问题?
private static string TestSendQualtricsSurvey()
{
string apiUrl = @"https://eu.qualtrics.com/API/v3/distributions";
string listId = "ML_123123123";
string surveyId = "SV_123123123";
string libraryId = "UR_123123123";
string messageId = "MS_123123123";
string senderName = "Survey Sender";
string senderEmail = "testing@email.com";
string emailSubject = "Please take this survey";
string contactId = "MLRP_123123123";
string apiKey = "123123123";
Survey survey = new Survey();
survey.surveyLink = new SurveyLink() { surveyId = surveyId, type = "Individual" };
survey.header = new Header() { fromEmail = senderEmail, fromName = senderName, subject = emailSubject };
survey.message = new Message() { libraryId = libraryId, messageId = messageId };
survey.recipients = new Recipients() { contactId = contactId, mailingListId = listId };
survey.sendDate = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ");
using (var ms = new MemoryStream())
{
var js = new DataContractJsonSerializer(typeof(Survey));
js.WriteObject(ms, survey);
ms.Position = 0;
var sr = new StreamReader(ms);
string surveyJson = sr.ReadToEnd();
using (var client = new WebClient())
{
client.Headers[HttpRequestHeader.ContentType] = "application/json";
client.Headers.Add("X-API-TOKEN", apiKey);
string response;
try
{
//IT HANGS HERE
response = client.UploadString(apiUrl, "POST", surveyJson);
return response;
}
catch (WebException ex)
{
using (var reader = new StreamReader(ex.Response.GetResponseStream()))
{
response = reader.ReadToEnd();
}
return response;
}
}
}
}