简体   繁体   中英

can't connect to azure rest web service

I am trying to develop a client program that creates a device identity thanks to azure . im using azure rest to create it so i call this webservice from client programme using jersey implementation but i get error com.sun.jersey.api.client.ClientHandlerException: java.net.SocketException: Socket is not connected: connect i test it using postman it work and python it work to . here is my java code :

public class Test {

    public static void main(String[] args) {

        try {

            Client client = Client.create();

            WebResource webResource = client
                    .resource("https://xxxx-iot-hub.azure-devices.net/devices");

            ClientResponse response =     webResource.path("/iotdevice1").queryParam("top", "100").queryParam("api-version", "2016-02-03").header("Content-Type", "application/json")
                    .header("Authorization", "SharedAccessSignature sr=xxxxx-iot-hub.azure-devices.net&sig=Yxxxxxxxxxx=1497357420&skn=iothubowner")
                    .put(ClientResponse.class);



            String output = response.getEntity(String.class);

            System.out.println("Output from Server .... \n");
            System.out.println(output);

        } catch (Exception e) {

            e.printStackTrace();

        }
    }

}

Thanks

According to your code, it seems that you want to create a new device identity using the REST API with HTTP PUT Method.

However, in your code, the query parameter top=100 is not necessary and the request body {deviceId: "iotdevice1"} is missing.

Here is my working code.

String body = "{deviceId: \"iotdevices1\"}";
ClientResponse response = webResource.path("/iotdevices1").queryParam("api-version", "2016-02-03")
                    .header("Content-Type", "application/json")
                    .header("Authorization",
                            "SharedAccessSignature sr=xxxx.azure-devices.net&sig=xxxxxxxx&se=1497357420&skn=iothubowner")
                    .put(ClientResponse.class, body);

Hope it helps. Any concern, please feel free to let me know.


Update :

For deleting a existing device identity, please see the REST API reference and see the code below.

ClientResponse response = webResource.path("/iotdevices1").queryParam("api-version", "2016-02-03")
                    .header("Content-Type", "application/json")
                    .header("If-Match", "*")
                    .header("Authorization",
                            "SharedAccessSignature sr=xxxx.azure-devices.net&sig=xxxxxx&se=1497490976&skn=iothubowner")
                    .delete(ClientResponse.class);

Please take note of the header If-Match above.

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