我创建了一个Web服务,我希望返回数据为JSON。在实际的web服务中,我向一个返回json格式的api服务发出请求,并使用字符串变量捕获它,但认为使用正确的配置设置,这将转换为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,我仍然不确定。