Вот код, который я пробовал:
Код: Выделить всё
public class Program
{
public static async Task Main(string[] args)
{
string serviceUrl = "soapapiurl"; // Replace with your SOAP API endpoint
string boundary = "----=_Part_" + Guid.NewGuid().ToString();
// Read the attachment file
string filePath = @"D:\C#Projects\CarFinanceFiles\NETRoadmap.pdf";
byte[] fileBytes = File.ReadAllBytes(filePath);
string fileContentId = "NETRoadmap.pdf";
// Construct the SOAP envelope
string soapEnvelope = @"
PAY
NETRoadmap
cid:" + fileContentId + @"
";
// Create the multipart content
var multipartContent = new MultipartContent("related", boundary);
// Add the SOAP envelope part
var soapContent = new StringContent(soapEnvelope, Encoding.UTF8, "text/xml");
// Important: Set Content-ID for the SOAP part if needed
soapContent.Headers.ContentType.Parameters.Add(new NameValueHeaderValue("type", "\"application/soap+xml\""));
multipartContent.Add(soapContent);
// Add the attachment part
var attachmentContent = new ByteArrayContent(fileBytes);
attachmentContent.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
attachmentContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = Path.GetFileName(filePath),
// The Content-ID should match the cid used in the SOAP envelope
Name = Path.GetFileName(filePath)
};
attachmentContent.Headers.Add("Content-ID", $"");
multipartContent.Add(attachmentContent);
using (HttpClient client = new HttpClient())
{
// Set headers
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/xml"));
client.DefaultRequestHeaders.Add("SOAPAction", "SendDocument");
// Send the request
HttpResponseMessage response = await client.PostAsync(serviceUrl, multipartContent);
if (response.IsSuccessStatusCode)
{
string responseContent = await response.Content.ReadAsStringAsync();
Console.WriteLine("Response received:");
Console.WriteLine(responseContent);
}
else
{
Console.WriteLine($"Error: {response.StatusCode}");
string errorContent = await response.Content.ReadAsStringAsync();
Console.WriteLine(errorContent);
}
}
}
}
Но это сработало через SOAPUI, включив MTOM в значение true. Пожалуйста, помогите мне`
Подробнее здесь: https://stackoverflow.com/questions/790 ... re-using-h