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 = @"003 GREATER KAILASH akul 1245678900 Lajpat Nagar New Delhi3010018732 22-03-16 88.89 [email protected] Water Bottle 1Ltr P259 35.56 1 DRY FS Apple-Blueberry PH01115 53.33 1 DRY ";
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.
Nishant MittalPosted May 9, 2016, 11:05 PM