WCF: The remote server returned an error: (400) Bad Request.

May 9 2016 9:01 AM
I have created a wcf Service. The Signature of the service is as.
 
[OperationContract(Name = "CustomerBillingDetail")]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json,
UriTemplate = "/CustomerBillingDetail/BillDetails")]
string CustomerBillingDetail(RemoteFileInfo objRemoteFileInfo);
public class RemoteFileInfo //: IDisposable
{
//[DataMember(Order = 2)]
public int ClientId;
//[DataMember(Order = 1)]
//public System.IO.Stream FileByteStream;
public byte[] FileByte;
//public void Dispose()
//{
// if (FileByteStream != null)
// {
// FileByteStream.Close();
// FileByteStream = null;
// }
//}
}
 
I am using the method as a rest client on the UI. The UI code is as.
 
public class RemoteFileInfo //: IDisposable
{
public int ClientId;
//public System.IO.Stream FileByteStream;
public byte[] FileByte;
//public void Dispose()
//{
// if (FileByteStream != null)
// {
// FileByteStream.Close();
// FileByteStream = null;
// }
//}
}
 
 
private void GetResponseForLS()
{
try
{
string url = "http://localhost/BillingService/BillingRestService/CustomerBillingDetail/BillDetails";
string SampleXML = @"<Root xmlns=""urn:microsoft-dynamics-nav/xmlports/x50000""><Trans_Header><Store_Code>003</Store_Code><Store_Name>GREATER KAILASH</Store_Name><Name>akul</Name><Mobile>1245678900</Mobile><DoB /><Address>Lajpat Nagar New Delhi</Address><Gender /><Bill_No>3010018732</Bill_No><Coupen_Code /><Order_Type /><Bill_Date>22-03-16</Bill_Date><Bill_Amt>88.89</Bill_Amt><Email_ID>[email protected]</Email_ID><Channel_ID /><Trans_Sales_Line><Product_Name>Water Bottle 1Ltr</Product_Name><Product_ID>P259</Product_ID><Item_Amt>35.56</Item_Amt><Qty>1</Qty><Is_Combo /><Item_Type>DRY</Item_Type></Trans_Sales_Line><Trans_Sales_Line><Product_Name>FS Apple-Blueberry</Product_Name><Product_ID>PH01115</Product_ID><Item_Amt>53.33</Item_Amt><Qty>1</Qty><Is_Combo /><Item_Type>DRY</Item_Type></Trans_Sales_Line></Trans_Header></Root>";
SampleXML = RemoveAllNamespaces(SampleXML);
// declare httpwebrequet wrt url defined above
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url);
// set method as post
webrequest.Method = "POST";
// set content type
webrequest.ContentType = "application/json";
var dataContarctSerializer = new DataContractSerializer(typeof(RemoteFileInfo));
var remoteFielInfo = new RemoteFileInfo()
{
ClientId = 2,
FileByte = Encoding.UTF8.GetBytes(SampleXML)
};
using(var memoryStream = new MemoryStream())
{
using (var reader = new StreamReader(memoryStream))
{
dataContarctSerializer.WriteObject(memoryStream, remoteFielInfo);
memoryStream.Position = 0;
string body = reader.ReadToEnd();
using (var streamWriter = new StreamWriter(webrequest.GetRequestStream()))
{
streamWriter.Write(body);
}
}
}
WebResponse webresponse = webrequest.GetResponse();
Encoding enc = System.Text.Encoding.GetEncoding("utf-8");
// read response stream from response object
StreamReader loResponseStream =
new StreamReader(webresponse.GetResponseStream(), enc);
// read string from stream data
string strResult = loResponseStream.ReadToEnd();
// close the stream object
loResponseStream.Close();
// close the response object
webresponse.Close();
}
catch (Exception ex)
{
throw ex;
}
}
 
when I am trying to execute that it is showing exception on  this line
WebResponse webresponse = webrequest.GetResponse(); and the error is "The remote server returned an error: (400) Bad Request."
 
Please help me to resolve this. It is urgent. 
 
 

Answers (1)