java - 使用 c 调用 REST 服务,通过 GSM 调制解调器连接 - HTTP/1.1 505

标签 java c rest tomcat microcontroller

我有一个遵循 Spring MVC 并使用 Tomcat-7.0.50 部署的 REST Web 服务 现在我想通过微 Controller 设备中的嵌入式代码来调用该服务。 GSM 调制解调器与设备一起托管,以通过 GPRS 建立连接。

这是用于调用部署在 tomcat 服务器中的 REST 服务的示例 C 代码。

char* connect_GPRS(void)
{
int cnt=0;

printf("connecting GPRS");
send_command("AT\r");
send_command("AT+CIPCLOSE\r");
send_command("AT+CIPSHUT\r");
send_command("AT+CIPCSGP=1,\"dialoguomlab\"\r");
send_command("AT+CGATT=1\r");
send_command("AT+CIPMODE=0\r");

send_command("AT+CIPSTART=\"TCP\",\"10.8.155.16\",\"8080\"\r"); // connect TCP
_delay_ms(1000);
send_command("AT+CIPSEND\r");
while ((strchr(modem_buf,'>')==NULL)&&(cnt<60)) //wait
{_delay_ms(100); cnt++; }
clr_buf();  

// send web request to the webserver
fprintf(&modem,"POST /MyService/addnums HTTP/1.1\r\n\
Host: 10.8.155.16:8080\r\n\
Content-Type: text/html\r\n\
Content-length:6\r\n\r\n\
    10,100\
    %c",26);

while(modem_buf[0]==0);  // wait for any data to come
_delay_ms(5000);
printf(modem_buf);
send_command("AT+CIPCLOSE\r");

_delay_ms(5000);
printf(modem_buf);   // check if anything in the buffer

return modem_buf;
}

Web 服务托管在 url http://10.8.155.16:8080/MyService 下 (ip 10.8.155.16只是一个示例,不是实际使用的,我们可以连接tomcat服务器)

这里是 REST 服务的代码。

@RequestMapping(value = "/addnums", method = RequestMethod.POST, headers = "Accept=text/html")
public @ResponseBody
String addNumbers(@RequestBody String request) {
    try{

    System.out.println("just enter to addnums.... " + request);

    int sum = 0;
    List<String> values = Arrays.asList(request.split(","));
    for (String s : values) {
        sum = sum + Integer.parseInt(s);
    }
    String sumAsString = Integer.toString(sum);
    System.out.println("going to send the response");
    return sumAsString;
    }catch(Exception e){
        System.out.println("Exception occured while processing..."+e.getMessage());
        return "Error";
    }
}

但是当调用这个时我们得到了错误

HTTP/1.1 505 HTTP Version Not Supported Server: Apache-Coyote/1.1 Date: Fri, 27 Jun 2014 04:45:27 GMT Connection: close

请帮助我确定根类(class)。

最佳答案

我们能够找到我自己的问题的答案并发帖,因为它可能对其他人有帮助。

我们能够通过单独的 TCP 服务器程序捕获请求,并且在调试请求行时我们看到设备在每个\n 之前添加了另一个\r。那么对于所有此类事件,字符串都有\r\r\n 。这就是程序无法运行的原因。因此,当通过删除\r 更改字符串时,问题就解决了。

    // send web request to the webserver
    fprintf(&modem,"POST /MyService/addnums HTTP/1.1\n\
    Host: 10.8.155.16:8080\n\
    Content-Type: text/html\n\
    Content-length:6\n\n\
    10,100\
    %c",26);

关于java - 使用 c 调用 REST 服务,通过 GSM 调制解调器连接 - HTTP/1.1 505,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24462040/

相关文章:

java - 在 TextArea Java 中移动文本光标

c - C中将字符串添加到char数组并用\0分隔

java - 使用 jasper Restapiv2 创建输入控件

c - YUV420 到 RGB 颜色转换错误

php - 如何在 Laravel 5 中验证 RESTful API?

node.js - 如何通过 Fitness REST API 从 Google 健身商店获取每日步数和活跃卡路里

java - 安卓/Java : detect if device has a BACK Camera

java - 为什么这违反了 Java 中的命名约定?

java - 如何在 Eclipse 中抑制 SQL Scrapbook "save resource"对话框

C printf整数类型U32