2
我看到有同一个问题有很多答案,但我无法解决我的问题,请你们中的任何一位请通过我的代码并解决我的问题。 我已创建WCF服务如下 POST方法WCF Post方法,返回400错误请求
接口(iComplaints.cs)
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json, UriTemplate = "/insertcomplaint")]
Stream InsertComplaint(ComplaintData data);
类(Complaints.cs)
public Stream InsertComplaint(ComplaintData data)
{
//the code
}
DataContract类(ComplaintData)
public class ComplaintData
{
[DataMember]
public string ComplaintID { get; set; }
[DataMember]
public string EntryBy { get; set; }
}
我已经托管了服务loc盟友,当我尝试使用下面的客户端方法它给我的400(错误请求)的WCF服务
<service behaviorConfiguration="ServiceBehavior" name="Complaints">
<endpoint address="" behaviorConfiguration="webHttp" binding="webHttpBinding" contract="IComplaints" />
<endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
</service>
请任何人的帮助
void PostComplaint()
{
HttpWebRequest req = null;
HttpWebResponse res = null;
string url = "http://localhost:28522/Complaints.svc/insertcomplaint";
ComplaintData iData = new ComplaintData();
iData.ComplaintID = txtComplaintID.Text;
iData.EntryBy = txtEntryBy.Text;
req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/json";
req.Headers.Add("SOAPAction", url);
using (var streamWriter = new StreamWriter(req.GetRequestStream()))
{
streamWriter.Write(iData);
}
res = (HttpWebResponse)req.GetResponse();
using (var streamReader = new StreamReader(res.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
TextBox1.Text = result;
}
}
Web配置来使用它。
@Sajeetharan:认真没...我知道我在做,如果你能发现什么地方错了。 –