简体   繁体   中英

android - wcf rest service communication

doing a communication between android client and wcf self-hosted service. Everything works perfect if I send post in Fiddler to the service, but android client gives back "java.net.SocketException: No route to host" when I try to send post. Connecting from real device through wifi to the pc with running service. Did anybody have this issue?

Server:

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

Client:

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.xx is in the private IP address range, not accessible from the public internet. If you are trying to connect to there from an Android device that is not on the same private network, it will fail with the error given.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM