在android上使用WCF服务 - 无效的主机名

问题描述:

我需要在我的android应用程序中调用WCF服务。对于通信我使用OkHttp库。在桌面浏览器服务工作正常的网址是这样的:在android上使用WCF服务 - 无效的主机名

http://localhost:7915/GeoService.svc/GetRoute?source=kyjevska&target=cyrila 

这里是WCF合同::

[ServiceContract] 
public interface IGeoService 
{ 
    [OperationContract] 
    [WebInvoke(Method ="GET", UriTemplate = "/GetRoute?source={source_addr}&target={target_addr}", 
    BodyStyle = WebMessageBodyStyle.Bare)] 
    Stream GetRoute(string source_addr, string target_addr); 
} 

服务代码:

public Stream GetRoute(string source_addr, string target_addr) 
    { 
     ... 
     ... 
     byte[] resultBytes = Encoding.UTF8.GetBytes(result); 
     WebOperationContext.Current.OutgoingResponse.ContentType = "text/plain"; 
     return new MemoryStream(resultBytes); 
    } 

和web.config中:http://pastebin.com/fXDh52x4

在我的解决方案目录中我发现applicationhost.config其中绑定了我的WCF站点:

<sites> 
    <site name="WebSite1" id="1" serverAutoStart="true"> 
     <application path="/"> 
      <virtualDirectory path="/" physicalPath="%IIS_SITES_HOME%\WebSite1" /> 
      </application> 
     <bindings> 
      <binding protocol="http" bindingInformation=":8080:localhost" /> 
     </bindings> 
    </site> 
    <site name="GeocodeWCF" id="2"> 
     <application path="/" applicationPool="Clr4IntegratedAppPool"> 
      <virtualDirectory path="/" physicalPath="E:\Bachelor\GeocodeAPI\GeocodeWCF" /> 
     </application> 
     <bindings> 
      <binding protocol="http" bindingInformation="*:7915:localhost" /> 
     </bindings> 
    </site> 
    <site name="WcfService1" id="3"> 
     <application path="/" applicationPool="Clr4IntegratedAppPool"> 
      <virtualDirectory path="/" physicalPath="E:\Bachelor\GeocodeAPI\WcfService1" /> 
     </application> 
     <bindings> 
      <binding protocol="http" bindingInformation="*:8745:localhost" /> 
     </bindings> 
    </site> 
     <siteDefaults> 
      <logFile logFormat="W3C" directory="%IIS_USER_HOME%\Logs" /> 
      <traceFailedRequestsLogging directory="%IIS_USER_HOME%\TraceLogFiles" enabled="true" maxLogFileSizeKB="1024" /> 
     </siteDefaults> 
     <applicationDefaults applicationPool="Clr4IntegratedAppPool" /> 
     <virtualDirectoryDefaults allowSubDirConfig="true" /> 
    </sites> 

由OkHttp其返回无效请求的Android呼叫 - 请求主机名无效

@Override 
protected String doInBackground(String... params) { 

    try { 
     encodedSourceAddress = URLDecoder.decode(params[0], "UTF-8"); 
     encodedTargetAddress = URLDecoder.decode(params[1], "UTF-8"); 
    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
    } 
    OkHttpClient client = new OkHttpClient(); 

    StringBuilder builder = new StringBuilder(); 

    builder.append(service); 
    builder.append("source=" + encodedSourceAddress); 
    builder.append("&target=" + encodedTargetAddress); 

    Request request = new Request.Builder().url(builder.toString()).build(); 

    Response response = null; 
    try { 
     response = client.newCall(request).execute(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    try { 
     result = response.body().string(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return result; 
} 

你能帮我解决这个问题吗?

我用COMMANDE IPCONFIG/ALL并找到LAN适配器的IPv4地址: enter image description here

网址像这样工作的:

http://192.168.182.1:7915/SomeService.svc/SomeFunction?source=xxx&target=xxx 

如果仍然没有工作,尝试在IIS管理器中设置绑定,applicationhost。 config,并为该端口添加防火墙规则