0

我试图从我的应用程序调用一个简单的 WCF 服务,但我总是收到错误请求的错误 400。

这是我在 appcelerator 中的代码:

var xhr = Titanium.Network.createHTTPClient();
xhr.setTimeout(1200);

xhr.onerror = function(e) {
    if (xhr.status != 200) {
        alert("The service is currently unavailable. Please Try Again Later. \nStatus:" + xhr.status + ". "+ xhr.statusText);
        return;
    }
};

xhr.onload = function() {
    var response = JSON.parse(this.responseText);
};

var request_url = "http://192.168.1.130/wcfTestPubb/wcfTest.Service1.svc/HelloWorld";
xhr.setRequestHeader("enctype", "multipart/form-data");
xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8");
xhr.open("GET", request_url);
xhr.send();

这是我的 WCF:

IService1.cs

public interface IService1 {
    [OperationContract]
    [WebInvoke(Method = "GET", UriTemplate = "HelloWorld/", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
    string HelloWorld();
}

服务1.cs

public class Service1 : IService1 {
    public string HelloWorld()
    {
        return "HELLO WORLD!";
    }     
}

网页配置

<?xml version="1.0"?>
    <configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
  </appSettings>
  <system.web>
    <compilation debug="true"/>
  </system.web>
  <system.serviceModel>
    <services>
      <service name="wcfTest.Service1">
        <host>
          <baseAddresses>
            <add baseAddress="http:///Design_Time_Addresses/wcfTest/Service1/"/>
          </baseAddresses>
        </host>
        <!-- Service Endpoints -->
        <endpoint address="" binding="wsHttpBinding" contract="wcfTest.IService1">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <!-- Metadata Endpoints -->
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/>
          <serviceDebug includeExceptionDetailInFaults="False"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup>    </configuration>

这是服务跟踪查看器的结果:

<TraceData>
    <DataItem>
        <TraceRecord Severity="Information" Channel="Analytic" xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord">
            <TraceIdentifier>http://msdn.microsoft.com/it-IT/library/System.ServiceModel.Diagnostics.TraceHandledException.aspx</TraceIdentifier>
            <Description>Throwing an exception. Exception details: System.ServiceModel.ProtocolException: There is a problem with the XML that was received from the network. See inner exception for more details. ---> System.Xml.XmlException: The body of the message cannot be read because it is empty. --- End of inner exception stack trace ---   in System.ServiceModel.Channels.HttpPipeline.EnqueueMessageAsyncResult.CompleteParseAndEnqueue(IAsyncResult result)   in System.ServiceModel.Channels.HttpPipeline.EnqueueMessageAsyncResult.HandleParseIncomingMessage(IAsyncResult result)   in System.Runtime.AsyncResult.SyncContinue(IAsyncResult result)   in System.ServiceModel.Channels.HttpPipeline.EmptyHttpPipeline.BeginProcessInboundRequest(ReplyChannelAcceptor replyChannelAcceptor, Action dequeuedCallback, AsyncCallback callback, Object state)   in System.ServiceModel.Channels.HttpChannelListener`1.HttpContextReceivedAsyncResult`1.ProcessHttpContextAsync()</Description>
            <AppDomain>/LM/W3SVC/1/ROOT/wcfTestPubb-11-130221681396205807</AppDomain>
            <Exception>
                <ExceptionType>System.ServiceModel.ProtocolException, System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType>
                <Message>There is a problem with the XML that was received from the network. See inner exception for more details.</Message>
                <StackTrace>[...]</StackTrace>
                <ExceptionString>System.ServiceModel.ProtocolException: There is a problem with the XML that was received from the network. See inner exception for more details. ---> System.Xml.XmlException: The body of the message cannot be read because it is empty. --- End of inner exception stack trace ---   in System.ServiceModel.Channels.HttpPipeline.EnqueueMessageAsyncResult.CompleteParseAndEnqueue(IAsyncResult result)   in System.ServiceModel.Channels.HttpPipeline.EnqueueMessageAsyncResult.HandleParseIncomingMessage(IAsyncResult result)   in System.Runtime.AsyncResult.SyncContinue(IAsyncResult result)   in System.ServiceModel.Channels.HttpPipeline.EmptyHttpPipeline.BeginProcessInboundRequest(ReplyChannelAcceptor replyChannelAcceptor, Action dequeuedCallback, AsyncCallback callback, Object state)   in System.ServiceModel.Channels.HttpChannelListener`1.HttpContextReceivedAsyncResult`1.ProcessHttpContextAsync()</ExceptionString>
                <InnerException>
                    <Exception>
                        <ExceptionType>System.Xml.XmlException, System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType>
                        <Message>The body of the message cannot be read because it is empty.</Message>
                        <StackTrace>[...]</StackTrace>
                        <ExceptionString>System.Xml.XmlException: The body of the message cannot be read because it is empty.</ExceptionString>
                    </Exception>
                </InnerException>
            </Exception>
        </TraceRecord>
    </DataItem>
</TraceData>

有人可以帮助我吗?我尝试了一切,但没有任何效果!:(

4

2 回答 2

0

尝试

var request_url = "http://192.168.1.130/wcfTestPubb/wcfTest.Service1.svc/HelloWorld/";

代替

var request_url = "http://192.168.1.130/wcfTestPubb/wcfTest.Service1.svc/HelloWorld";
于 2013-08-28T13:30:02.077 回答
0

从 IService1.cs 中删除这部分,然后检查:

BodyStyle = WebMessageBodyStyle.WrappedRequest

或使用

BodyStyle = WebMessageBodyStyle.Bare

你用提琴手测试过吗?检查这里: http: //pantestmb.blogspot.com/search/label/POST 和这里:http ://www.mehdi-khalili.com/fiddler-in-action/part-1

于 2013-08-30T06:53:27.977 回答