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

标签 android rest wcf http

我需要在我的 android 应用程序中调用 WCF 服务。对于通信,我正在使用 OkHttp 库。在桌面浏览器服务中,在这样的 url 上工作正常:

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

这里是 WCF 契约(Contract)::

[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,其中绑定(bind)了我的 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 调用返回Bad Request - The request hostname is invalid:

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

你能帮我解决一下吗?

最佳答案

我使用命令 ipconfig/all 并找到 LAN 适配器的 ipv4 地址: enter image description here

这样的 url 有效:

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

如果还是不行,尝试在 IIS 管理器、applicationhost.config 中设置绑定(bind),并为该端口添加防火墙规则

关于android - 在 Android 上使用 WCF 服务 - 主机名无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42607159/

相关文章:

javascript - 使用 WCF REST 服务和 Javascript 中的流以及 URL 参数上传文件

php - WCF 服务的前端

mysql - 如何分离这两个过程?

android - 无法在 Android 上执行 'postMessage' 消息上的 'DOMWindow'

Android更改默认按钮背景颜色

android - 在android上使用ionic modal时如何禁用后退按钮?

java - 带有 javax.ws.rs.core.Application 的 MultiPartFeature

session - session 真的违反了 RESTful 原则吗?

android - 如何防止回收的 ListView 项目显示旧内容?

java - 如何将 JSON 解析相关错误映射到 Jersey 中的 json 响应