从安卓客户端向WCF WS发送两个参数

本文关键字:参数 两个 客户端 WS WCF | 更新日期: 2023-09-27 18:31:39

>我正在尝试开发一个项目,其中我的Android应用程序将IP地址和端口号发送到WCF Web服务:但我做不到并且有可能获得 Web 服务的响应???是服务.cs

        [OperationContract]
    [WebGet(UriTemplate = "Connection/{ipadress}/{port}")]
    bool Connection(string ipadress,string port);

Service.svc.cs

    public bool Connection(string ipadress, string port)
    {
        /*          */
            return true;
    }

代码安卓

    public boolean Connection_Net() {
    String url_service = "http://algotimesheet.azurewebsites.net/Service1.svc/Connection/ipadress/port";
    URLConnection urlConnection;
    DataOutputStream dataOutputStream;
    DataInputStream dataInputStream;
    URL url;
    try {
        url = new URL(url_service);
        urlConnection = url.openConnection();
        urlConnection.setDoOutput(true);
        urlConnection.setDoInput(true);
        urlConnection.setUseCaches(false);
        urlConnection.setRequestProperty("Content-Type", "application/json");
        urlConnection.setRequestProperty("Host", "android.schoolportal.gr");
        urlConnection.connect();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("ipadress","192.168.1.1");
        jsonObject.put("port","4370");
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(urlConnection.getOutputStream());
        outputStreamWriter.write(jsonObject.toString());
        outputStreamWriter.close();

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return true;
}

从安卓客户端向WCF WS发送两个参数

如果你发送一个JSON,你需要编写服务来期待一个对象并使用POST

[OperationContract]
[WebInvoke(Method="POST", 
    RequestFormat = WebMessageFormat.Json, 
    UriTemplate = "Connection")]
bool Connection(Data data);
public bool Connection(Data data)
{
    /*          */
        return true;
}

并像这样设计对象:

public class Data()
{
    public string ip {get; set;}
    public string port{get;set;}
}

如果要进行GET只需将值添加到URL