gpt4 book ai didi

http - 如何使用 arduino ethernet shield 和 web 客户端保持 HTTP session

转载 作者:可可西里 更新时间:2023-11-01 16:43:05 25 4
gpt4 key购买 nike

我想从我的网站获取 json。我正在尝试发送 POST 请求进行身份验证,然后我想发送 GET 请求以获取 json,但我做不到,因为它给我 en error 401 unauthorized (cookies doesn't saved)。我怎样才能与 Arduino、以太网屏蔽和 WebClient 保持 session ?

来源:

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

byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

//Change to your server domain
char serverName[] = "bh.quickle.me";

// change to your server's port
int serverPort = 80;

// change to the page on that server
char pageName[] = "/users/sign_in";
char sensorPage[] = "/api/v1/sensors";

EthernetClient client;
int totalCount = 0;
// insure params is big enough to hold your variables
char params[68];

// set this to the number of milliseconds delay
// this is 30 seconds
#define delayMillis 30000UL

unsigned long thisMillis = 0;
unsigned long lastMillis = 0;

void setup() {
Serial.begin(9600);

// disable SD SPI
pinMode(4,OUTPUT);
digitalWrite(4,HIGH);
pinMode(7,OUTPUT);
digitalWrite(7,HIGH);

Serial.print(F("Starting ethernet..."));
if(!Ethernet.begin(mac)) Serial.println(F("failed"));
else Serial.println(Ethernet.localIP());
delay(2000);
Serial.println(F("Ready"));
sprintf(params,"{\"user\": {\"email\": \"EgorkZe@gmail.com\",\"password\": \"1234asdf\"}}");
postPage(serverName,serverPort,pageName,params);

}

void loop()
{
thisMillis = millis();

if(thisMillis - lastMillis > delayMillis)
{
lastMillis = thisMillis;
getPage(serverName, serverPort, sensorPage);
}
}

byte postPage(char* domainBuffer,int thisPort,char* page,char* thisData)
{
int inChar;
char outBuf[64];

Serial.print(F("connecting..."));

if(client.connect(domainBuffer,thisPort))
{
Serial.println(F("connected"));

// send the header
sprintf(outBuf,"POST %s HTTP/1.1",page);
client.println(outBuf);
sprintf(outBuf,"Host: %s",domainBuffer);
client.println(outBuf);
client.println(F("Connection: close\r\nContent-Type: application/json"));
sprintf(outBuf,"Content-Length: %u\r\n",strlen(thisData));
client.println(outBuf);

// send the body (variables)
client.print(thisData);
}
else
{
Serial.println(F("failed"));
return 0;
}

int connectLoop = 0;

while(client.connected())
{
while(client.available())
{
inChar = client.read();
Serial.write(inChar);
connectLoop = 0;
}

delay(1);
connectLoop++;
if(connectLoop > 10000)
{
Serial.println();
Serial.println(F("Timeout"));
client.stop();
}
}

Serial.println();
Serial.println(F("disconnecting."));
client.stop();
return 1;
}

byte getPage(char* domainBuffer, int thisPort, char* page)
{
int inChar;
char outBuf[128];

Serial.print(F("connecting..."));

if(client.connect(domainBuffer,thisPort))
{
Serial.println(F("connected"));

sprintf(outBuf,"GET %s HTTP/1.1",page);
client.println(outBuf);
sprintf(outBuf,"Host: %s",serverName);
client.println(outBuf);
client.println(F("Connection: close\r\n"));
}
else
{
Serial.println(F("failed"));
return 0;
}

// connectLoop controls the hardware fail timeout
int connectLoop = 0;

while(client.connected())
{
while(client.available())
{
inChar = client.read();
Serial.write(inChar);
// set connectLoop to zero if a packet arrives
connectLoop = 0;
}

connectLoop++;

// if more than 10000 milliseconds since the last packet
if(connectLoop > 10000)
{
// then close the connection from this end.
Serial.println();
Serial.println(F("Timeout"));
client.stop();
}
// this is a delay for the connectLoop timing
delay(1);
}

Serial.println();

Serial.println(F("disconnecting."));
// close client end
client.stop();

return 1;
}

最佳答案

首先,是的,Arduino 以太网能够存储和发回 cookie。

您必须从身份验证请求中捕获 cookie,并在第二个 HTTP 请求(GET 请求)的 header 中将它们发回。

查看有关RFC HTTP 协议(protocol)的信息

您还可以查看: http://en.wikipedia.org/wiki/HTTP_cookie#Setting_a_cookie

关于http - 如何使用 arduino ethernet shield 和 web 客户端保持 HTTP session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23107495/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com