【发布时间】:2018-06-20 02:45:06
【问题描述】:
我目前正在尝试将一些数据发布到服务器以创建用户配置文件。 RESTful POST 的 body 已经选择了 image 和其他字段。
我设法使用 Postman 发布它。但是,我试图弄清楚如何使用 C# 来做到这一点。我尝试过,但似乎服务器返回状态 500 并没有太多有用的消息。
请参阅有关我如何 POST 到服务器的附件和一些有关我如何尝试 POST 的 C# 代码。感谢有关如何修复我的 C# 代码以使其正常工作的帮助。
C#代码:
if (!string.IsNullOrEmpty(baseUrl) && !string.IsNullOrEmpty(apiKey))
{
var fullUrl = baseUrl + "/user_profiles";
using (var httpClient = new HttpClient())
{
//httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", apiKey);
httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + apiKey);
var boundary = "Upload----" + DateTime.Now.Ticks.ToString();
using (var content = new MultipartFormDataContent(boundary))
{
try
{
var name = string.IsNullOrEmpty(userRegistration.Name) ? "" : userRegistration.Name;
var city = string.IsNullOrEmpty(userRegistration.City) ? "" : userRegistration.City.ToUpper();
var phone = string.IsNullOrEmpty(userRegistration.Mobile) ? "" : userRegistration.Mobile;
phone = phone.Replace(" ", ""); //remove empty string in between and at edges
phone = phone.StartsWith("+") ? phone : "+" + phone;
var address = string.IsNullOrEmpty(userRegistration.CompanyAddress) ? "Unspecified" : userRegistration.CompanyAddress;
var country = string.IsNullOrEmpty(userRegistration.Country) ? "" : userRegistration.Country.ToUpper();
var email = string.IsNullOrEmpty(userRegistration.Email) ? "" : userRegistration.Email;
var isContractor = userRegistration.IsRegisteredAsContractor.ToString();
var category = string.IsNullOrEmpty(userRegistration.Category) ? "" : userRegistration.Category;
var companyName = string.IsNullOrEmpty(userRegistration.CompanyName) ? "" : userRegistration.CompanyName;
var companyAddress = string.IsNullOrEmpty(userRegistration.CompanyAddress) ? "" : userRegistration.CompanyAddress;
//TEST
//var body = new
//{
// city,
// phone,
// address,
// country,
// email,
// name,
// identifier_for_vendor = localId,
// is_contractor = isContractor,
// work_categories = category,
// company_name = companyName,
// company_address = companyAddress
//};
//var bodyStr = JsonConvert.SerializeObject(body);
//var stringContent = new StringContent(bodyStr, Encoding.UTF8, "application/json");
//content.Add(stringContent);
//response = await httpClient.PostAsync(fullUrl, content).ConfigureAwait(false);
//TEST
content.Add(new StringContent(name, Encoding.UTF8, "text/plain"), "name");
content.Add(new StringContent(city, Encoding.UTF8, "text/plain"), "city");
content.Add(new StringContent(phone, Encoding.UTF8, "text/plain"), "phone");
content.Add(new StringContent(companyAddress, Encoding.UTF8, "text/plain"), "address");
content.Add(new StringContent(country, Encoding.UTF8, "text/plain"), "country");
content.Add(new StringContent(email, Encoding.UTF8, "text/plain"), "email");
content.Add(new StringContent(localId, Encoding.UTF8, "text/plain"), "identifier_for_vendor");
content.Add(new StringContent(isContractor, Encoding.UTF8, "text/plain"), "is_contractor");
content.Add(new StringContent(category, Encoding.UTF8, "text/plain"), "work_categories");
content.Add(new StringContent(companyName, Encoding.UTF8, "text/plain"), "company_name");
content.Add(new StringContent(companyAddress, Encoding.UTF8, "text/plain"), "company_address");
if (!string.IsNullOrEmpty(userRegistration.ProfileImagePath) && File.Exists(userRegistration.ProfileImagePath))
{
using (var stream = File.OpenRead(userRegistration.ProfileImagePath))
{
//stream.Seek(0, SeekOrigin.Begin);
var fileName = Guid.NewGuid().ToString() + ".jpg";
content.Add(new StreamContent(stream), "image", fileName);
response = await httpClient.PostAsync(fullUrl, content).ConfigureAwait(false);
}
}
else
{
response = await httpClient.PostAsync(fullUrl, content).ConfigureAwait(false);
}
}
catch (Exception ex)
{
}
}
}
}
【问题讨论】:
标签: c# rest post xamarin.forms dotnet-httpclient