Menu

 

Wednesday, January 26, 2011

Posting Information Using HttpWebRequest In WCF

Few days ago I have post Information Using HttpWebRequest To WCF I Will Share The code and Way To get Xml for Posting.

I Searched Net I was Not able to get better Example of Posting Xml Soap Envolpe to WCF.

So Let Start.

What I have Used is Vs2010

Get Soap XML

When You Run WCF Project u Will Get WCF Test Client.




You Will See WCF Test Client

2} When U Will Run Method You Will Get The Follwing Out Put

This is Normal Scenario

3} Know We Will Get Xml For Posting (When u Will Click XML Tab) then U Will See


Know U want Post Information

Copy Xml From Request


<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://tempuri.org/IService1/GetData</Action>
</s:Header>
<s:Body>
<GetData xmlns="http://tempuri.org/">
<value>2</value>
</GetData>
</s:Body>
</s:Envelope>

Know We Will Move To c# Code



public string CreatteSoapRequest(string strSoapBodyInfo)
{

string strSoapvalue =@"<?xml version=""1.0"" encoding=""utf-8""?><soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/""><soap:Body>" + strSoapBodyInfo + "</soap:Body></soap:Envelope>";
return strSoapvalue;
}


public void PostDataGetData()
{

/// You Take care 2 Things 1 Soapaction and Other Soap Envolvpe

string strvalue = "1";
string MethodXml = @"<GetData xmlns=""http://tempuri.org/""><value>1</value></GetData>";


string soap = CreatteSoapRequest(MethodXml);


HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://localhost:65371/Service1.svc");
req.Headers.Add("SOAPAction", "\"http://tempuri.org/IService1/GetData\"");
req.ContentType = "text/xml;charset=\"utf-8\"";
req.Accept = "text/xml";
req.Method = "POST";

using (Stream stm = req.GetRequestStream())
{
using (StreamWriter stmw = new StreamWriter(stm))
{
stmw.Write(soap);
}
}

WebResponse response = req.GetResponse();
byte[] result = null;
string strvalue = response.GetResponseStream().ToString();
Stream responseStream = response.GetResponseStream();
// TODO: Do whatever you need with the response
int byteCount = Convert.ToInt32(response.ContentLength);

using (BinaryReader reader = new BinaryReader(response.GetResponseStream()))
{

result = reader.ReadBytes(byteCount);

}
SaveStreamToFile(result, "D://GetData.txt");
}

/// This test Method Which I Have Created for Wrting text file on hardDisk
private void SaveStreamToFile(Byte[] stream, String fileName)
{
FileStream file = new FileStream(fileName, FileMode.Create);

file.Write(stream, 0, stream.Length);

file.Flush();

file.Close();

}

for Composite U can Find The data In Same Way..

Sample Xml String is Below .

string MethodXml = @"<GetDataUsingDataContract xmlns=""http://tempuri.org/""> <composite xmlns:d4p1=""http://schemas.datacontract.org/2004/07/PostDataUsingWCF"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><d4p1:BoolValue>true</d4p1:BoolValue><d4p1:StringValue>Avinash</d4p1:StringValue></composite></GetDataUsingDataContract>";