HttpClient发送请求时动态替换目标ip

问题描述:
使用HttpClient进行http请求,每次请求随机使用一个target host进行请求。

发http请求的时候,一般会配置数据源,设置ClientPNames.DEFAULT_HOST,这样在请求的时候目标机器host和端口就是配置的ClientPNames.DEFAULT_HOST。但是不能每次请求ClientPNames.DEFAULT_HOST,因为一个httpclient就对应一个ClientPNames.DEFAULT_HOST,就无法针对一次request进行目标host的修改。

例如:
HttpGet httpGet = new HttpGet(url);
在进行请求时,httpGet作为参数执行execute,此时url是相对路径,httpclient会将defaulthost+uri拼接为完整的请求地址进行请求。

看HttpClient的源代码可以发现URI是个线索,在execute方法中会判断URI中设置的是否是绝对路径还是相对路径,如果是相对路径时httpclient会将defaulthost+uri拼接为完整的请求地址。如果URI中设置了scheme并且URI配置了host就会用URI完整的绝对路径作为请求地址。判断逻辑如下

public final HttpResponse execute(HttpUriRequest request,
                                  HttpContext context)
    throws IOException, ClientProtocolException {

    if (request == null) {
        throw new IllegalArgumentException
            ("Request must not be null.");
    }

    return execute(determineTarget(request), request, context);
}

private static HttpHost determineTarget(HttpUriRequest request) throws ClientProtocolException {
    // A null target may be acceptable if there is a default target.
    // Otherwise, the null target is detected in the director.
    HttpHost target = null;

    URI requestURI = request.getURI();
    if (requestURI.isAbsolute()) {
        target = URIUtils.extractHost(requestURI);
        if (target == null) {
            throw new ClientProtocolException(
                    "URI does not specify a valid host name: " + requestURI);
        }
    }
    return target;
}
分析完就找到了方法解决问题了,在请求时设置URI即可,要注意的是build URI的时候要配置scheme参数才能生效。

要在ESP32上发送POST请求,您需要使用ESP32的WiFi功能连接到互联网,并使用HTTP客户端库发送POST请求。下面是一个示例代码,可以在ESP32上使用Arduino IDE进行编写和上传: ```c #include <WiFi.h> #include <HTTPClient.h> // Replace with your network credentials const char* ssid = "your_SSID"; const char* password = "your_PASSWORD"; // Your Domain name with URL path or IP address with path const char* serverName = "http://yourdomain.com/api/post_data"; void setup() { //Initialize serial and wait for port to open: Serial.begin(115200); while (!Serial) { ; // wait for serial port to connect. Needed for native USB port only } // We start by connecting to a WiFi network Serial.println(); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); } void loop() { // Use WiFiClient class to create TCP connections WiFiClient client; HTTPClient http; Serial.print("[HTTP] begin...\n"); // configure server and url http.begin(client, serverName); Serial.print("[HTTP] POST...\n"); // start connection and send HTTP header int httpResponseCode = http.POST("Hello World"); // httpCode will be negative on error if (httpResponseCode > 0) { // HTTP header has been send and Server response header has been handled Serial.printf("[HTTP] POST... code: %d\n", httpResponseCode); // file found at server if (httpResponseCode == HTTP_CODE_OK) { String payload = http.getString(); Serial.println(payload); } } else { Serial.printf("[HTTP] POST... failed, error: %s\n", http.errorToString(httpResponseCode).c_str()); } http.end(); delay(10000); } ``` 将上面的示例代码中的“your_SSID”和“your_PASSWORD”替换为您的WiFi网络凭据,并将“serverName”替换为您要发送POST请求的URL或IP地址。然后,您可以上传并运行代码,ESP32将连接到WiFi网络并发送POST请求
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值