gpt4 book ai didi

java - android - WCF Rest服务通信

转载 作者:行者123 更新时间:2023-12-01 15:47:41 24 4
gpt4 key购买 nike

在android客户端和wcf自托管服务之间进行通信。如果我在 Fiddler 中将帖子发送到服务,一切都会完美,但是当我尝试发送帖子时,Android 客户端会返回“java.net.SocketException:没有到主机的路由”。通过 wifi 从真实设备连接到正在运行服务的电脑。有人遇到过这个问题吗?

服务器:

[ServiceContract]
public interface ISDMobileService
{
[OperationContract]
[WebInvoke(Method="POST",BodyStyle=WebMessageBodyStyle.Bare,ResponseFormat=WebMessageFormat.Xml,RequestFormat=WebMessageFormat.Xml)]
string PostMessage(string SdaMessage);
}

public class Service : ISDMobileService
{
public string PostMessage(string SdaMessage)
{
Console.WriteLine( "Post Message : " + SdaMessage );
return"Calling Post for you " + SdaMessage;
}
}

客户:

String urlToSendRequest = "http://172.16.3.4:7310/PostMessage";
String targetDomain = "172.16.3.4";

HttpClient httpClient = new DefaultHttpClient();
HttpPost request = new HttpPost(urlToSendRequest);

List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("SdaMessage", "param value one"));

request.addHeader("Content-Type", "application/xml");

try
{
request.setEntity(new UrlEncodedFormEntity(postParameters));
HttpResponse response = httpClient.execute(request);

if(response != null)
{
HttpParams str = response.getParams();
}
}
catch (Exception ex)
{
ex.printStackTrace();
}

最佳答案

172.16.x.x 位于私有(private) IP 地址范围内,无法从公共(public)互联网访问。如果您尝试从不在同一专用网络上的 Android 设备连接到那里,它将失败并出现给出的错误。

关于java - android - WCF Rest服务通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6819362/

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