【问题标题】:Intel Galileo Azure REST call英特尔 Galileo Azure REST 调用
【发布时间】:2017-07-02 10:11:44
【问题描述】:

我正在尝试通过调用作为 Web 应用程序托管在 Azure 中的 Microsoft Web API(REST 服务)来利用我的 Intel Galileo Gen2 但我得到的只是一个 azure 404 说它找不到我的应用程序

我的arduino上的代码如下

#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
char serverName[] = "wack.azurewebsites.net";

// Initialize the Ethernet client library
// with the IP address and port of the server 
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    while(true);
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");

  // if you get a connection, report back via serial:  
  if (client.connect(serverName, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.println("GET /api/warning HTTP/1.0");
    client.println();
  } 
  else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

void loop()
{
  // if there are incoming bytes available 
  // from the server, read them and print them:
  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

    // do nothing forevermore:
    while(true);
  }
}

我尝试多次更改目标网址

 client.println("GET wack.azurewebsites.net/api/warning HTTP/1.0");
 client.println("GET api/warning HTTP/1.0");
 client.println("GET /api/warning ");
 client.println("GET /api/warning HTTP/1.1");

它们都不起作用,有些甚至无法连接 但是它只返回 404,即使该 URL 是有效的,并且在不同的应用程序中甚至在同一网络上都经过测试。

我已将 mac 地址替换为设备的地址,并且之前已确认通过将其设置为 Web 服务器本身可以在网络中访问它。基本上它可以抓取互联网设置,但由于某种原因无法连接到我的休息服务。任何形式的帮助表示赞赏。谢谢

【问题讨论】:

    标签: rest azure arduino httpclient intel-galileo


    【解决方案1】:

    问题在于没有为通话提供主持人。我来自 c# 背景,并认为通过使用服务器地址初始化 HttpClient 就足够了,但显然你必须提供这一行

    client.println("GET /api/warning HTTP/1.1");
    client.println("Host: wack.azurewebsites.net");
    client.println("User-Agent: Intel Galileo");
    client.println("Connection: close\r\n");
    

    每次您提出请求时

    【讨论】:

    • Host 标头是由 HTTP/1.1 协议规定的,它不是特定于语言的:developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Host
    • 但我使用 http/1.0 协议发送
    • Azure 应用服务不支持 HTTP/1.0。为此,每个网络应用程序都需要自己的公共 IP 地址,这在今天是一个完全不合理的提议,更不用说 HTTP/1.0 是 Gopher 时代的协议(1991-1996)。
    • Marcu - 希望你不介意,我编辑了你的答案以添加完整的请求。
    【解决方案2】:

    您缺少 Host 标头。

    Host header is mandated by HTTP/1.1 protocol specification,您必须在每个请求中发送它。

    您还应该发送User-AgentConnection: close 成为一个好的HTTP 公民。后者告诉网络服务器“不要保持套接字打开,因为我会马上挂断”。 HTTP/1.1 默认为Connection: keep-alive

    将您的请求修改为如下所示:

    client.println("GET /api/warning HTTP/1.1");
    client.println("Host: wack.azurewebsites.net");
    client.println("User-Agent: Intel Fakelileo");
    client.println("Connection: close\r\n");
    

    一个简单的测试:

    $ telnet {sitename}.azurewebsites.net 80                              
    
    Connected to waws-prod-am2-119.cloudapp.net.
    Escape character is '^]'.
    GET / HTTP/1.1
    Host: {sitename}.azurewebsites.net
    User-agent: Intel Fakelileo
    Connection: close
    
    
    HTTP/1.1 200 OK
    Content-Length: 84
    Content-Type: text/plain; charset=utf-8
    Server: Microsoft-IIS/8.0
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-18
      • 1970-01-01
      • 1970-01-01
      • 2018-02-21
      • 1970-01-01
      • 2018-04-20
      • 1970-01-01
      • 2011-05-17
      相关资源
      最近更新 更多