0

我编写了一个 WCF 服务,它在浏览器中运行良好!和提琴手一样。但是当我试图在 android 中使用这个服务时。状态码是 400。我已经尝试了几乎所有的方法!但没有运气。请引导我正确的方向。

在安卓中:

public String fetch(String param)
{
    StringBuilder stringBuilder = new StringBuilder();
    HttpClient httpClient = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(param);
    httpGet.setHeader("Accept", "application/json");
    httpGet.setHeader("Content-Type","application/json");
    try {
            HttpResponse response = httpClient.execute(httpGet);
            StatusLine statusLine = response.getStatusLine();
            int statusCode = statusLine.getStatusCode();
            if (statusCode == 200) {
                HttpEntity entity = response.getEntity();
                InputStream inputStream = entity.getContent();
                BufferedReader reader = new BufferedReader(
                new InputStreamReader(inputStream));
                String line;
                while ((line = reader.readLine()) != null) {
                        stringBuilder.append(line);
                }

                inputStream.close();
                return line;
            } else {
                Log.d("fetch", "Failed to download");
            }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

在 WCF 中:

[ServiceContract]
public interface IService
{
    [OperationContract]
    [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "json/getsong/{name}")]
    string getsong(string name);
}

网络配置:

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

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>
    <services>
      <service behaviorConfiguration="MusicServiceBehaviour" name="WcfService.MusicService">
        <endpoint address="" behaviorConfiguration="web" binding="webHttpBinding"
          contract="WcfService.IService" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="MusicServiceBehaviour">
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="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="false"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>

</configuration>
4

0 回答 0