从appcelerator调用WCF服务时出现错误400 Bad Request

时间:2013-08-27 13:27:41

标签: android wcf service appcelerator bad-request

我正在尝试从我的应用程序调用一个简单的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();
}

Service1.cs

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

的Web.config

<?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>
有人可以帮帮我吗?我尝试了一切,但没有任何工作! :(

2 个答案:

答案 0 :(得分: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";

答案 1 :(得分: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