gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 17:28:08 26 4
gpt4 key购买 nike

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com