2011-07-25 87 views
0

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

服务器:

[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

172.16.x.x是私有IP地址范围,而不是通过公共互联网访问。如果您尝试通过不在同一专用网络上的Android设备连接到该设备,则会因给出的错误而失败。

+0

谢谢主席先生,这解决了我的问题。 – Maxim

+0

如果我们想测试从Android设备到WCF的请求,IP会是什么? – kavita