I have WCF service to store and retrieve messages (in/from a database). I'm calling it from ASP.NET.
When I run my app I face with following problem:
The remote server returned an error: (400) Bad Request.
I don't actually understand how to cope with it.
Here's the class that throws the exception:
public class SendMessageClient
{
private string BASE_URL = "http://localhost:49350/Service1.svc/";
public bool sendMessage(Message message)
{
try
{
DataContractSerializer ser = new DataContractSerializer(typeof(Message));
MemoryStream mem = new MemoryStream();
ser.WriteObject(mem, message);
string data = Encoding.UTF8.GetString(mem.ToArray(), 0, (int)mem.Length);
WebClient webClient = new WebClient();
webClient.Headers["Content-Type"] = "application/json";
webClient.Encoding = Encoding.UTF8;
webClient.UploadString(BASE_URL + "sendmessage", "POST", data);
return true;
}
catch
{
return false;
}
}
}
Here's stack trace
at System.Net.HttpWebRequest.GetResponse() at System.Net.WebClient.GetWebResponse(WebRequest request) at System.Net.WebClient.DownloadBits(WebRequest request, Stream writeStream) at System.Net.WebClient.UploadBits(WebRequest request, Stream readStream, Byte[] buffer, Int32 chunkSize, Byte[] header, Byte[] footer) at System.Net.WebClient.UploadDataInternal(Uri address, String method, Byte[] data, WebRequest& request) at System.Net.WebClient.UploadString(Uri address, String method, String data) at System.Net.WebClient.UploadString(String address, String method, String data) at MessageServiceClient.Models.SendMessageClient.sendMessage(Message message) in C:\Users\Asus\source\repos\Web service project\MessageServiceClient\Models\SendMessageClient.cs:line 27
What I have tried:
I checked my WCF Service with reqbin and it works with 200 OK inserting data to my db