[英]WCF Restful post Services returns BAD REQUEST(400)
I am trying from few days to consume wcf restful service but it is giving me bad reqeust. 我正在尝试从几天开始消费wcf宁静的服务,但它给了我糟糕的要求。 Please Help me out. 请帮帮我。
Here is my configuration file 这是我的配置文件
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="state" allowCookies="true">
<security mode="None"></security>
</binding>
</webHttpBinding>
</bindings>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehaviour">
<serviceMetadata httpGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="True"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="ServiceBehaviour" name="RESTFUL_DEMO.Web.services.Calc">
<endpoint address="" bindingConfiguration="state" binding="webHttpBinding" name="Http" contract="RESTFUL_DEMO.Web.services.ICalc"/>
<endpoint address="abcd" binding="wsHttpBinding" name="wsHttp" contract="RESTFUL_DEMO.Web.services.ICalc"/>
<endpoint address="mex" binding="mexHttpBinding" name="MEX" contract="IMetadataExchange"/>
</service>
</services>
my interface for service contract and datacontract is as follows. 我的服务合同和数据合同界面如下。
[ServiceContract(SessionMode = SessionMode.Allowed)]
[XmlSerializerFormat]
public interface ICalc
{
[OperationContract]
[WebInvoke(UriTemplate = "dowork", Method = "POST", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare)]
int DoWork(Enroll a);
}
[DataContract]
public class Enroll
{
public Enroll()
{
}
public Enroll(string Avalue)
{
this.Avalue = Avalue;
}
[DataMember(IsRequired = true)]
public string Avalue
{
get;
set;
}
}
my code to consume the service is as follows 我使用该服务的代码如下
HttpWebRequest request = WebRequest.Create("http://localhost/RESTFUL_DEMO.Web/services/Calc.svc/dowork") as HttpWebRequest;
XmlDocument doc = new XmlDocument();
doc.Load(@"d:\test.xml");
string sXML = doc.InnerXml;
request.ContentLength = sXML.Length;
request.ContentType = "test/xml; charset=utf-8";
var sw = new StreamWriter(request.GetRequestStream());
sw.Write(sXML);
sw.Close();
WebResponse response = request.GetResponse();
StreamReader stream = new StreamReader(response.GetResponseStream());
String result = stream.ReadToEnd();
You have made a small mistake while consuming Rest service. 在使用Rest服务时你犯了一个小错误。 You have specified ContentType of request to test /xml; 您已将testType的ContentType指定为test / xml; charset=utf-8 but it should be text/xml or application/xml. charset = utf-8但它应该是text / xml或application / xml。
request.ContentType = "text/xml; charset=utf-8";
or it should be 或它应该是
request.ContentType = "application/xml";
In my case, my method in service interface IBookService.cs is like below 在我的例子中,我在服务接口IBookService.cs中的方法如下所示
[OperationContract]
[WebInvoke(Method = "PUT", UriTemplate = "/Book", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
IList<Book> UpdateBook(Book book);
and in my client I was giving 在我的客户里,我正在给予
client.Headers[HttpRequestHeader.ContentType] = "text/xml";
instead of 代替
client.Headers[HttpRequestHeader.ContentType] = "text/json";
That fixed my problem. 这解决了我的问题。 Please see below for my complete solution 请参阅下面的完整解决方案
private void btnUpdateBook_Click(object sender, EventArgs e)
{
try
{
using(WebClient client = new WebClient())
{
client.Headers[HttpRequestHeader.ContentType] = "text/json";
Uri uri = new Uri(@"http://localhost:8085/BookService/Book");
Book updateBook = new Book() { Id = 3, Name = "UpdateBook Name 3", Price = 77.77f };
MemoryStream requestStream = new MemoryStream();
DataContractJsonSerializer requestSerializer = new DataContractJsonSerializer(typeof(Book));
requestSerializer.WriteObject(requestStream, updateBook);
client.UploadDataCompleted += OnUpdateBookCompleted;
client.UploadDataAsync(uri, "PUT",requestStream.ToArray());
}
}
catch (Exception ex)
{
}
}
void OnUpdateBookCompleted(object sender, UploadDataCompletedEventArgs e)
{
byte[] result = e.Result as byte[];
MemoryStream responseStream = new MemoryStream(result);
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(IList<Book>));
IList<Book> books = (IList<Book>)serializer.ReadObject(responseStream);
bindingSource1.DataSource = books;
dvBooks.DataSource = bindingSource1;
}
Get the service up and running in an instance of Visual Studio, then use the Test Client to make sure the service is running OK. 在Visual Studio实例中启动并运行该服务,然后使用Test Client确保服务正常运行。
Open a new instance of VS and add a service reference which will build the client code for you, then use this client to call the service. 打开VS的新实例并添加一个服务引用,它将为您构建客户端代码,然后使用此客户端来调用该服务。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.