[英]can't connect to azure rest web service
我正在尝试开发一个客户端程序,该程序可以通过azure创建设备标识。 即时通讯使用azure rest来创建它,所以我使用jersey实现从客户端程序调用此webservice,但出现错误com.sun.jersey.api.client.ClientHandlerException:java.net.SocketException:套接字未连接:连接我使用它进行测试邮递员的工作和python它的工作。 这是我的Java代码:
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();
}
}
}
谢谢
根据您的代码,您似乎想使用带有HTTP PUT方法的REST API创建新的设备标识。
但是,在您的代码中,不需要查询参数top=100
并且缺少请求正文{deviceId: "iotdevice1"}
。
这是我的工作代码。
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);
希望能帮助到你。 如有任何疑问,请随时告诉我。
更新 :
有关删除现有设备标识的信息,请参阅REST API参考和以下代码。
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);
请注意上面的标题If-Match
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.