【发布时间】:2015-04-03 17:23:52
【问题描述】:
我是 WCF 新手。我正在尝试从我的 aspx 页面调用 WCF 以发布 xml 并返回 json。我的服务运行良好,但我收到“远程服务器返回错误:(400 )错误的请求“。也许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);
【问题讨论】:
-
这个怎么样?当你打开你的 web 服务所在的 url 时,它会显示你对 web 服务的描述吗? -
感谢您的回复,但它还不能正常工作......对不起
-
request.ContentType = "application/xml;charset=utf-8"; “应用程序”拼写错误......这是否是您的问题,我不确定。澄清一下……您的意思是服务器端服务可以正常工作并经过测试吗?
-
谢谢 Nathan..但是这里仍然有 400 人...
-
信息不足。您能否将您的 WCF 服务实现添加到问题详细信息中,尤其是 postJson 方法?