2012-06-06 119 views
0

我已經創建了一個Web服務,我希望返回數據在JSON中。在實際的web服務中,我向一個返回json格式的api服務發出請求,並用一個字符串變量捕獲它,但認爲使用正確的配置設置,它將被轉換爲json。請求服務。WCF服務不返回正確JSON

 request.Accept = "text/javascript, text/html, application/xml, text/xml, */*" 
     request.Headers.Set(HttpRequestHeader.AcceptLanguage, "en-us,en;q=0.5") 
     request.Headers.Set(HttpRequestHeader.AcceptEncoding, "gzip,deflate") 
     request.Headers.Set(HttpRequestHeader.AcceptCharset, "ISO-8859-1,utf-8;q=0.7,*;q=0.7") 
     request.Headers.Add("Keep-Alive", "115") 
     request.KeepAlive = True 
     request.Headers.Add("X-Requested-With", "XMLHttpRequest") 
     request.Headers.Add("X-Prototype-Version", "1.6.1") 
     request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8" 
     request.Credentials = New System.Net.NetworkCredential(username, password) 
     request.Method = "GET" 

     response = DirectCast(request.GetResponse(), HttpWebResponse) 
     Dim reader As New StreamReader(response.GetResponseStream()) 
     tmp = reader.ReadToEnd() 

合同服務

<OperationContract()> 
<WebInvoke(Method:="GET", Responseformat:=WebMessageFormat.Json)> 
Function RetrieveData(ByVal streamData As String) As String 

和web.config文件:

<configuration> 
<system.web> 
<compilation debug="true" /> 
</system.web> 
<system.serviceModel> 
<services> 
    <service name="WcfInstanceRules2.Service1" behaviorConfiguration ="mex"> 
    <!-- Service Endpoints --> 
    <endpoint address ="" 
     binding="webHttpBinding" 
     contract="WcfInstanceRules2.IService1" behaviorConfiguration ="JsonBehavior"> 
     <identity> 
     <dns value="localhost"/> 
     </identity> 
    </endpoint> 
    <!-- Metadata Endpoints --> 
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> 
    </service> 
</services> 
<behaviors> 
    <serviceBehaviors> 
    <behavior name="mex"> 
     <serviceMetadata httpGetEnabled="true"/> 
     <serviceDebug includeExceptionDetailInFaults="true" /> 
    </behavior> 
    </serviceBehaviors> 
    <endpointBehaviors> 
    <behavior name="JsonBehavior"> 
     <webHttp /> 
    </behavior> 
    </endpointBehaviors> 
</behaviors> 

讚賞任何想法。服務的結果必須是真正的JSON,所以我可以將它用作JSONP,但我仍然不確定。

回答