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

时间:2017-03-05 10:24:19

标签: android rest wcf http

我需要在我的Android应用程序中调用WCF服务。为了通信我正在使用OkHttp库。在桌面浏览器服务中,这样的URL工作正常:

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

你能帮我解决一下吗?

1 个答案:

答案 0 :(得分:1)

我使用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中设置绑定,并为该端口添加防火墙规则