android - wcf休息服务通信

时间:2011-07-25 16:32:23

标签: java android wcf

在android客户端和wcf自托管服务之间进行通信。如果我将Fiddler中的帖子发送到服务,一切都很完美,但是当我尝试发送帖子时,android客户端会返回“java.net.SocketException:No route to host”。 通过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(); 
}

1 个答案:

答案 0 :(得分:1)

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