2017-03-05 3 views
0

Ich brauche WCF-Dienst in meiner Android-Anwendung aufrufen. Zur Kommunikation verwende ich die OkHttp-Bibliothek. In Desktop-Browser-Dienst funktioniert auf URL wie diese fein:Consume WCF-Dienst auf Android - Ungültiger Hostname

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

hier sind WCF Vertrag ::

[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); 
} 

Servicecode:

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); 
    } 

und web.config: http://pastebin.com/fXDh52x4

in meinem Lösungsverzeichnis Ich fand applicationhost.config, wo ist verbindlich für meine WCF-Site:

<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> 

Android Anruf von OkHttp die Bad Request zurückkehren - Die Anfrage Hostname ist ungültig:

@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; 
} 

Können Sie mir helfen, es zu beheben bitte?

Antwort

0

Ich benutze commande ipconfig/all und IPv4-Adresse des LAN-Adapters finden: enter image description here

url funktioniert wie folgt:

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

Wenn immer noch nicht funktioniert, versuchen Satz Bindungen in IIS-Manager applicationhost. konfigurieren und Firewall-Regel für diesen Port hinzufügen

Verwandte Themen