Tôi có một dịch vụ WebAPI mà đọc bài dữ liệu bao gồm dữ liệu tập tin sử dụngbài viết dữ liệu sử dụng MultipartFormDataContent từ HttpClient
HttpContext.Request as key value pairs
HttpContext.Current.Request["LoadId"]
Bây giờ tôi đang cố gắng để viết và ứng dụng Console sử dụng HttpClient
nhưng không thể làm cho nó hoạt
private static bool AddException(string vin, string loadId, string sessionId)
{
var client = new HttpClient
{
BaseAddress = new Uri("url")
};
ServicePointManager.ServerCertificateValidationCallback = (s, cert, chain, ssl) => true;
const string quickCode = "01-01-1";
const string inspectionType = "Loading";
const string inspectorCode = "001";
const string numberOfImages = "1";
const string imageNumber = "1";
const string exceptionType = "Driver";
const string imageType = "exception";
var date = DateTime.Now.ToString();
var content = new MultipartFormDataContent();
var values = new[]
{
new KeyValuePair<string, string>("LoadId", loadId),
new KeyValuePair<string, string>("VIN", vin),
new KeyValuePair<string, string>("SessionId", sessionId),
new KeyValuePair<string, string>("QuickCode", quickCode),
new KeyValuePair<string, string>("strInspectionType", inspectionType),
new KeyValuePair<string, string>("InspectorCode", inspectorCode),
new KeyValuePair<string, string>("NoOfImages", numberOfImages),
new KeyValuePair<string, string>("Imageno", imageNumber),
new KeyValuePair<string, string>("strExceptionType", exceptionType),
new KeyValuePair<string, string>("ImageType", imageType),
new KeyValuePair<string, string>("DateTimeOffset", date)
};
var fileContent = new ByteArrayContent(File.ReadAllBytes(@"C:\Users\Public\Pictures\Sample Pictures\Desert.jpg"));
fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "Desert.jpg"
};
content.Add(fileContent, "file", "11");
foreach (var keyValuePair in values)
{
content.Add(new StringContent(keyValuePair.Value), keyValuePair.Key);
}
var response = client.PostAsync("Exception/AddException", content).Result;
var exceptionResult = response.Content.ReadAsAsync<bool>().Result;
return exceptionResult;
}
Ở trên là mã. Nhưng không có khả năng đọc mã từ các dịch vụ
tôi đã không kiểm soát mã dịch vụ và không thể thay đổi nó
Bạn có nhận được bất kỳ ngoại lệ hoặc bất kỳ trả lời từ máy chủ? Bạn đã thử sử dụng Fiddler để gỡ lỗi chưa? –