Can anyone explain to me why im getting a http 400 error when trying to post to my webservice?
My service contract ::
[ServiceContract]
public interface IfldtWholesaleService {
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "MAC")]
string MAC(string input);
My call;
private void postToWebsite()
{
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(txtUrl.Text);
req.Method = "POST";
req.MediaType = "text/xml";
string input = "dfwa";
req.ContentLength = ASCIIEncoding.UTF8.GetByteCount(input);
StreamWriter writer = new StreamWriter(req.GetRequestStream());
writer.Write(input);
writer.Close();
var rsp = req.GetResponse().GetResponseStream();
txtOut.Text = new StreamReader(rsp).ReadToEnd();
}
My server config file
<system.serviceModel>
<services>
<service name="fldtRESTWebservice.fldtWholesaleService" behaviorConfiguration="httpBehaviour">
<endpoint address="" binding="webHttpBinding" contract="fldtRESTWebservice.IfldtWholesaleService" behaviorConfiguration="httpEndpointBehavour">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
<host>
<baseAddresses>
<add baseAddress="http://localhost:8080/ContactService/"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="httpBehaviour">
<serviceMetadata httpGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="False"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="httpEndpointBehavour">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
EDIT :: it also gives the same error when using MediaType “text/plain”
An endpoint with webHttpBinding / webHttp behavior by default accepts requests in either XML or JSON format. And the XML you send needs to conform with what the service expects. The code below sends a request which your service expects. Also, notice that you need to set the ContentType property on the HttpWebRequest, not MediaType.