C# 错误400-->;来自客户端的wcf错误请求
我是WCF新手。我正在尝试从我的aspx页面调用WCF来发布xml并返回json。我的服务运行良好,但我收到了一个错误“远程服务器返回了一个错误:(400)错误请求”。也许WCF的配置文件给了我这样一个错误响应。我尝试了很多,但困惑只会出现在我身上。请帮助我编写代码。。 我的客户端代码是C# 错误400-->;来自客户端的wcf错误请求,c#,asp.net,json,wcf,C#,Asp.net,Json,Wcf,我是WCF新手。我正在尝试从我的aspx页面调用WCF来发布xml并返回json。我的服务运行良好,但我收到了一个错误“远程服务器返回了一个错误:(400)错误请求”。也许WCF的配置文件给了我这样一个错误响应。我尝试了很多,但困惑只会出现在我身上。请帮助我编写代码。。 我的客户端代码是 string SampleXml = @"<parent>" + "<child>" + "<username&g
string SampleXml = @"<parent>" +
"<child>" +
"<username>username</username>" +
"<password>password</password>" +
"</child>" +
"</parent>";
//ASCIIEncoding encoding = new ASCIIEncoding();
string postData = SampleXml.ToString();
byte[] data = Encoding.UTF8.GetBytes(postData);
string url = "http://localhost:52573/Service1.svc/postjson/";
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Method = "POST";
request.ContentType = "applicatoin/xml;charset=utf-8";
//request.Accept = "application/json";
request.ContentLength = data.Length;
String test = String.Empty;
Stream newStream = request.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
test = reader.ReadToEnd();
reader.Close();
dataStream.Close();
}
Response.Write(test);
接口是
[OperationContract]
[WebInvoke(UriTemplate = "postjson",Method="POST",RequestFormat=WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Json,BodyStyle = WebMessageBodyStyle.Wrapped)]
string postjson(string streamdata);
经过长时间的斗争,输出来到这里。我没有更改WCF的web.config中的任何代码。这里是我的客户端代码
protected void Page_Load(object sender, EventArgs e)
{
// Restful service URL
string url = "http://localhost:52573/Service1.svc/postjson";
// declare ascii encoding
ASCIIEncoding encoding = new ASCIIEncoding();
string strResult = string.Empty;
// sample xml sent to Service & this data is sent in POST
string SampleXml = @"<parent>" +
"<child>" +
"<username>username</username>" +
"<password>password</password>" +
"</child>" +
"</parent>";
string postData = SampleXml.ToString();
// convert xmlstring to byte using ascii encoding
byte[] data = encoding.GetBytes(postData);
// declare httpwebrequet wrt url defined above
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url);
// set method as post
webrequest.Method = "POST";
// set content type
//webrequest.Accept = "application/xml;q=0.8";
//webrequest.ContentType = "application/xml;charset=utf-8";
webrequest.ContentType = "application/x-www-form-urlencoded";
// set content length
webrequest.ContentLength = data.Length;
// get stream data out of webrequest object
Stream newStream = webrequest.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
// declare & read response from service
HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();
// set utf8 encoding
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
strResult = loResponseStream.ReadToEnd();
// close the stream object
loResponseStream.Close();
// close the response object
webresponse.Close();
// below steps remove unwanted data from response string
Response.Write(strResult);
}
伙计们,这就是我所做的…我也清理了最近的文件…这个怎么样?当您打开web服务所在的url时,它是否会显示web服务的描述?感谢您的回复,buddy,但它还不起作用…sorryrequest.ContentType=“applicationin/xml;charset=utf-8”;“应用程序”拼写错误。。。这是不是你的问题,我不确定。为了澄清。。。你的意思是服务器端服务工作正常并且经过测试吗?谢谢Nathan…但是这里仍然有400人…这还不够信息。能否将WCF服务实现添加到问题细节中,尤其是postJson方法?
[OperationContract]
[WebInvoke(UriTemplate = "postjson",Method="POST",RequestFormat=WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Json,BodyStyle = WebMessageBodyStyle.Wrapped)]
string postjson(string streamdata);
protected void Page_Load(object sender, EventArgs e)
{
// Restful service URL
string url = "http://localhost:52573/Service1.svc/postjson";
// declare ascii encoding
ASCIIEncoding encoding = new ASCIIEncoding();
string strResult = string.Empty;
// sample xml sent to Service & this data is sent in POST
string SampleXml = @"<parent>" +
"<child>" +
"<username>username</username>" +
"<password>password</password>" +
"</child>" +
"</parent>";
string postData = SampleXml.ToString();
// convert xmlstring to byte using ascii encoding
byte[] data = encoding.GetBytes(postData);
// declare httpwebrequet wrt url defined above
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url);
// set method as post
webrequest.Method = "POST";
// set content type
//webrequest.Accept = "application/xml;q=0.8";
//webrequest.ContentType = "application/xml;charset=utf-8";
webrequest.ContentType = "application/x-www-form-urlencoded";
// set content length
webrequest.ContentLength = data.Length;
// get stream data out of webrequest object
Stream newStream = webrequest.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
// declare & read response from service
HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();
// set utf8 encoding
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
strResult = loResponseStream.ReadToEnd();
// close the stream object
loResponseStream.Close();
// close the response object
webresponse.Close();
// below steps remove unwanted data from response string
Response.Write(strResult);
}
public string postjson(Stream streamdata)
{
StreamReader reader = new StreamReader(streamdata);
string xmlString = reader.ReadToEnd();
string returnValue = new JavaScriptSerializer().Serialize(xmlString);
return returnValue.ToString();
}