I am trying to obtain the attached file from the following response of a Soap with attachments response.
HTTP/1.1 200 OK
Date: Mon, 22 Nov 2021 08:52:38 GMT
Server: Microsoft-IIS/5.0
Cache-Control: no-cache, private
Strict-Transport-Security: max-age=63072000; includeSubdomains; preload
X-XSS-Protection: 1;mode=block
Referrer-Policy: origin-when-cross-origin,strict-origin-when-cross-origin
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
Vary: User-Agent
Content-Type: multipart/related; type="text/xml"; charset=utf-8; boundary=e5dec305-c274-4dae-8cf4-23f373e1ef23; start="<null>
"
Response_time: D=1078981 t=1637571157195291
Keep-Alive: timeout=15, max=500
Connection: Keep-Alive
Transfer-Encoding: chunked
--e5dec305-c274-4dae-8cf4-23f373e1ef23
Content-Type: text/xml; charset=utf-8
Content-Transfer-Encoding: 8bit
Content-ID:
<null>
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://administracion.gob.es/punto-unico-notificaciones/respuestaPeticionAcceso" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<SOAP-ENV:Header>
<wsse:Security>
<wsu:Timestamp wsu:Id="Id-6b1b22fa-a028-4bb0-83f9-3d2ad9111c1b">
<wsu:Created>2021-11-22T08:52:38.000Z</wsu:Created>
<wsu:Expires>2021-11-22T08:57:38.000Z</wsu:Expires>
</wsu:Timestamp>
<wsse:BinarySecurityToken EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary" ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3" wsu:Id="CertId-3d2079c6-222b-4bab-88f6-33dd214c4f8d">MIIHrjCCBpagAwIBAgIQft7 0deAk5JhFQlRBPK9njANBgkqhki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5jZXJ0LmZubXQuZXMvY3Jsc2NvbXAvQ1JMMS5jcmwwDQYJKoZIhvcNAQELBQADggEBACSyNnj3Kd05oB7fScFj 2sy5CKtHryiCIwrlLAGHWS0rik20WSrx1 hgkRJxgsKvqH5mxDzddWfg93 NwEP6JCHPMtPqfWMaLHGuHQfRYxKJx00akufhgH2knpAEFJhB5SGEU0gQuRSijvF5 a VhX9evLt3RwnGYIwcPo34MTlr PHR7PTgzjGxYn/8g8etrOeGe3eZAIp0YNTDSUA1BLSdhDjM6zP7DdTOFJGcfu3pJAx3EpuEBaSQlyxc2FWDqWrP6xPKgz5VdqsJ4Zlekka0SRI ub/dNQ4I44Pj qs15FWSUF3Mt VBNizg2omHpdcccB2xpgBsBN5k0GzT50=</wsse:BinarySecurityToken>
<ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#">
<ds:SignedInfo>
<ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
<ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256" />
<ds:Reference URI="#Id-7ac15be4-4e93-439b-a3e2-3278ab3a6d32">
<ds:Transforms>
<ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
</ds:Transforms>
<ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
<ds:DigestValue>6Mg5Y=</ds:DigestValue>
</ds:Reference>
<ds:Reference URI="#Id-6b1b22fa-a028-4bb0-83f9-3d2ad9111c1b">
<ds:Transforms>
<ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
</ds:Transforms>
<ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
<ds:DigestValue>[...]</ds:DigestValue>
</ds:Reference>
</ds:SignedInfo>
<ds:SignatureValue>[...]</ds:SignatureValue>
<ds:KeyInfo>
<wsse:SecurityTokenReference xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wsse:Reference URI="#CertId-3d2079c6-222b-4bab-88f6-33dd214c4f8d" ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3" />
</wsse:SecurityTokenReference>
</ds:KeyInfo>
</ds:Signature>
</wsse:Security>
</SOAP-ENV:Header>
<SOAP-ENV:Body wsu:Id="Id-7ac15be4-4e93-439b-a3e2-3278ab3a6d32">[...]
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
--e5dec305-c274-4dae-8cf4-23f373e1ef23--
Content-Type: application/zip; charset=utf-8
Content-Transfer-Encoding: binary
Content-ID: [...]
BINARYDATA
I have obtained the response with this method
public HttpWebResponse SendRequest2(string Xmlrequest, string SOAPaction)
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(_endPoint);
webRequest.ServicePoint.Expect100Continue = true;
webRequest.Headers.Add("Use", "literal");
webRequest.Headers.Add("Parameters-style", "Wrapped");
webRequest.ContentType = "text/xml; charset=utf-8";
webRequest.Method = "POST";
webRequest.Connection = "Keep - Alive";
webRequest.Accept = "/*/";
webRequest.Headers.Add("SOAPAction", SOAPaction);
var content = Encoding.UTF8.GetBytes(Xmlrequest);
webRequest.ContentLength = content.Length;
using (var stream = webRequest.GetRequestStream())
{
stream.Write(content, 0, content.Length);
}
var response = (HttpWebResponse)webRequest.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
return responseString ;
}
I have tried unsuccessfully to capture the binarydata in a string variable, obtaining only gibberish. Havent found any good documentation on how to get the attachments on C#.
CodePudding user response:
In the end the reply provided here by Dhaval Kateliya was the way to go. I add here my version of the method.
public string SendRequestMultipart(string Xmlrequest, string SOAPaction)
{
try
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(_endPoint);
webRequest.ServicePoint.Expect100Continue = true;
var content = Encoding.UTF8.GetBytes(Xmlrequest);
webRequest.ContentLength = content.Length;
using (var stream = webRequest.GetRequestStream())
{
stream.Write(content, 0, content.Length);
}
var response = (HttpWebResponse)webRequest.GetResponse();
var contenttype = response.Headers.Get("Content-Type");
if (response.Headers.Get("Content-Type") != "text/xml; charset=utf-8")
{
String retVal = readMultipart(response).Result;
return retVal.ToString();
}
else
{
string answertring =
new StreamReader(response.GetResponseStream()).ReadToEnd();
return answertring ;
}
}
catch (WebException ex)
{
string error = GetSoapFault(ex);
return error;
}
async Task<string> readMultipart(HttpWebResponse httpResponse)
{
try
{
var content = new StreamContent(httpResponse.GetResponseStream());
content.Headers.Add("Content-Type", httpResponse.ContentType);
MultipartMemoryStreamProvider multipart = new MultipartMemoryStreamProvider();
Task.Factory.StartNew(() => multipart = content.ReadAsMultipartAsync().Result,
CancellationToken.None,
TaskCreationOptions.LongRunning, // guarantees separate thread
TaskScheduler.Default)
.Wait();
String json = await multipart.Contents[0].ReadAsStringAsync();
string path = @"mypath";
byte[] fileData = multipart.Contents[1].ReadAsByteArrayAsync().Result;
System.IO.File.WriteAllBytes(path, fileData);
return json;
}
catch (Exception ex)
{
return ex.Message;
}
}
}