【问题标题】:Californium Framework CoAP and PUT requestCalifornium 框架 CoAP 和 PUT 请求
【发布时间】:2016-03-23 00:28:00
【问题描述】:

我正在尝试使用 Californium 向 coap 服务器(er-rest-example)发出请求。 我成功地做了一个 POST 请求。 但是使用 PUT 我收到了一个错误的请求,我尝试在 url 中使用这个 URL:

coap://[aaaa::c30c:0000:0000:0002]:5683/actuators/leds
coap://[aaaa::c30c:0000:0000:0002]:5683/actuators/leds? 
coap://[aaaa::c30c:0000:0000:0002]:5683/actuators/leds?color=r

但是没有人获得成功。 我做错了什么?。

这是我的简单脚本:

package coap_client;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.Timer;
import java.util.TimerTask;

import org.eclipse.californium.core.CoapClient;
import org.eclipse.californium.core.CoapResponse;
import org.eclipse.californium.core.coap.MediaTypeRegistry;

public class cliente {
    public static void main(String[] args) throws Exception {
        Timer timer;
        timer = new Timer();
        TimerTask task = new TimerTask(){
                @Override
                public void run(){
                    String url="coap://[aaaa::c30c:0000:0000:0002]:5683/actuators/leds";
                    URI uri= null;
                    try {
                        uri = new URI(url);
                    } catch (URISyntaxException e) {
                        e.printStackTrace();
                    }
                    CoapClient client = new CoapClient(uri);
                    CoapResponse response = client.put("color=r",MediaTypeRegistry.TEXT_PLAIN);             
                    System.out.println(response.isSuccess());                   
                    if (response!=null) {
                        byte[] myreponse=response.getPayload();
                        String respuesta2 = new String(myreponse);
                        System.out.println(respuesta2);
                        }
                }
        };
        timer.schedule(task, 10,10*1000);
    }

}

【问题讨论】:

  • 您好,您可以在此处发布您的代码以发送发布请求吗?

标签: eclipse coap californium


【解决方案1】:

在 Contiki er-rest-example 中,查看 LED CoAP 资源的POST/PUT 处理程序(1)。它需要一个 mode 参数,没有它你会得到一个 BAD_REQUEST 作为响应。我认为这必须放在请求正文中。

【讨论】:

  • 非常感谢您的回复。我添加模式参数并解决问题!谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-14
  • 2013-06-12
  • 1970-01-01
  • 2018-12-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多