2017-03-05 75 views
0

我需要在我的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; 
} 

你能幫我解決這個問題嗎?

回答

0

我用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,併爲該端口添加防火牆規則

相關問題