我是WCF的新手。我试图从我的aspx页面调用WCF来发布xml并返回json.My服务运行良好,但我收到的错误是"远程服务器返回一个错误:(400)Bad Request"。可能WCF的配置文件给了我这样的错误响应。我尝试了很多,但只是让我感到困惑..请帮我代码中的人请... 我的客户端代码是
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);
WCF的Web.config文件代码是
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<authorization>
<allow users="?" />
</authorization>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
<identity impersonate="false" />
</system.web>
<system.serviceModel>
<services>
<service name="JsonandXmlService.Service1" behaviorConfiguration="myServiceBehavior">
<!--http://localhost:52573/Service1.svc/postjson/-->
<endpoint name="myServiceBehavior" address="" binding="webHttpBinding" contract="JsonandXmlService.IService1" behaviorConfiguration="webHttp">
</endpoint>
<endpoint name="mexHttpBinding" address="mex" binding="webHttpBinding" contract="IMetadataExchange" behaviorConfiguration="webHttp" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="myServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
<behavior>
<!--<!– To avoid disclosing metadata information,
set the value below to false and remove the metadata endpoint
above before deployment –>-->
<serviceMetadata httpGetEnabled="true" />
<!--<!– To receive exception details in faults for debugging purposes,
set the value below to true. Set to false before deployment
to avoid disclosing exception information –>-->
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="webHttp">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<protocolMapping>
<add binding="webHttpBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="false" />
<directoryBrowse enabled="true" />
</system.webServer>
</configuration>
我的服务文件在这里
public string postjson(string streamdata)
{
return new JavaScriptSerializer().Serialize(streamdata);
}
接口是
[OperationContract]
[WebInvoke(UriTemplate = "postjson",Method="POST",RequestFormat=WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Json,BodyStyle = WebMessageBodyStyle.Wrapped)]
string postjson(string streamdata);
答案 0 :(得分:0)
经过长时间的努力,输出来到这里..我没有改变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);
}
我的服务实现代码是
public string postjson(Stream streamdata)
{
StreamReader reader = new StreamReader(streamdata);
string xmlString = reader.ReadToEnd();
string returnValue = new JavaScriptSerializer().Serialize(xmlString);
return returnValue.ToString();
}
这就是我做过的人......我也清除了最近的文件......