HItesh Rana
asked on
Add attachment using HttpWebRequest in C#
I'm trying to call a Java web service with an attachment in C#.
I was able to test the web service in SOAP UI and I saw what fiddler was outputting in the request. I tried to mimic what fiddler was doing but I kept getting a 400 request error. By mimic meaning just manually add what is below in a string. I don't have the code for the attachment above. But basically I just add it after " stmw.Write(soap);" so " stmw.Write(attachment);" Is there a better way of doing this? What am I doing wrong?
string soap = "<?xml version='1.0' encoding='utf-8'?> <soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:soap='http://www.w3.org/2003/05/soap-envelope' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:int='http://xxxxx.com/'>" +
"<soap:Header/> " +
"<soap:Body> " +
" <int:xxxxxSubmission> " +
" xxxxxxxxxxxx " +
" </int:xxxxxSubmission> " +
"</soap:Body> " +
"</soap:Envelope> ";
X509Certificate2 cert = new X509Certificate2(@"C:\Test\xxxxxx.pfx", @"xxxxx");
X509Certificate2 cert2 = new X509Certificate2(@"C:\Test\xxx.cer", @"xxxxx");
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://xxxxx/TestService");
req.ContentType = "application/soap+xml;charset=\"utf-8\"";
req.Accept = "application/soap+xml";
req.Method = "POST";
req.Host = "test";
req.KeepAlive = true;
req.ClientCertificates.Add(cert);
req.ClientCertificates.Add(cert2);
using (Stream stm = req.GetRequestStream())
{
using (StreamWriter stmw = new StreamWriter(stm))
{
stmw.Write(soap);
stmw.Close();
}
}
WebResponse response = req.GetResponse();
Stream responseStream = response.GetResponseStream();
response = req.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream());
string result = sr.ReadToEnd();
sr.Close();
I was able to test the web service in SOAP UI and I saw what fiddler was outputting in the request. I tried to mimic what fiddler was doing but I kept getting a 400 request error. By mimic meaning just manually add what is below in a string. I don't have the code for the attachment above. But basically I just add it after " stmw.Write(soap);" so " stmw.Write(attachment);" Is there a better way of doing this? What am I doing wrong?
<soap:Envelope>
.....
</soap:Envelope>
------=_Part_29_14978990.1522017799648
Content-Type: text/plain; charset=us-ascii; name=attachment.txt
Content-Transfer-Encoding: 7bit
Content-ID: <attachment.txt>
Content-Disposition: attachment; name="attachment.txt"; filename="attachment.txt"
ISA
GS
ST
BHT
FDE
------=_Part_29_14978990.1522017799648--
Does "Add Service Reference" not work for you? It's pretty cumbersome to try an implement SOAP manually.
ASKER
That is a good question. I agree with you that is pretty cumbersome to do so. I'm just trying to get something working. I have used VS 2015 and added the wsdl file the client has given me. But then there I was having issues where 1) The payload was not being encrypted and 2) the attachment was not being added.
I created another post for that:
https://www.experts-exchange.com/questions/29091082/C-web-service-call-using-WCF-is-not-adding-oasis-security-header.html
I created another post for that:
https://www.experts-exchange.com/questions/29091082/C-web-service-call-using-WCF-is-not-adding-oasis-security-header.html
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.