byte[] iarBytes;
string sBoundary = "MIME_boundaryS0M3B0UnDAry2121";
//Load Message header
string sHead = File.ReadAllText(@"C:\Temp\Head.txt");
//Load Envelope tamplate
string sEnvelope = File.ReadAllText(@"C:\Temp\Envelope.xml");
//Pass parameters
sEnvelope = sEnvelope.Replace("###UserId###", txtUserId.Text);
sEnvelope = sEnvelope.Replace("###Func###", txtFunc.Text);
//Generate ContentId for attachment
Guid cid = Guid.NewGuid();
sEnvelope = sEnvelope.Replace("###ContentId###", cid.ToString());
string sAttachment = txtAttachment.Text;
//Create SOAP message
string sSOAP = "";
//Add boundary
sSOAP += "\r\n--" + sBoundary + "\r\n";
//Add header
sHead = sHead.Replace("###ContentLength###", sEnvelope.Length.ToString());
sSOAP += sHead;
//Add Envelope
sSOAP += sEnvelope;
//Add boundary
sSOAP += "\r\n--" + sBoundary + "\r\n";
//Add attachment
sSOAP += "Content-ID: <" + cid.ToString() + ">\r\n";
sSOAP += "Content-Length: " + sAttachment.Length.ToString() + "\r\n";
sSOAP += "Content-Type: application/binary\r\n\r\n";
sSOAP += sAttachment;
//Add boundary
sSOAP += "\r\n--" + sBoundary;
//Create web request
HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(@"http://xx.xx.xx.xx:xxxx/STWS/STExchangeWS.dll/soap/IST_ExchWebService");
wr.ReadWriteTimeout = 30000;
wr.Method = "POST";
wr.ContentType = @"multipart/related; boundary=MIME_boundaryB0R9532143182121";
//Convert string to bytes
Encoding enc = Encoding.GetEncoding(1257);
iarBytes = enc.GetBytes(sSOAP);
wr.ContentLength = iarBytes.Length;
//Send post
Stream str = wr.GetRequestStream();
str.Write(iarBytes, 0, iarBytes.Length);
str.Close();
//Get response
WebResponse resp = wr.GetResponse();
str = resp.GetResponseStream();
MemoryStream mstr = new MemoryStream();
iarBytes = new byte[1024];
int iBytesRead = 0;
do
{
iBytesRead = str.Read(iarBytes, 0, iarBytes.Length);
mstr.Write(iarBytes, 0, iBytesRead);
} while (iBytesRead != 0);
str.Close();
//Write response to file
iarBytes = mstr.ToArray();
mstr.Close();
File.WriteAllBytes(@"C:\Temp\soap.txt", iarBytes);
|