即使在标头中设置浏览器名称和 cookie 后仍收到 401 Unauthorized

Hug*_*ing 3 java rest http

我正在运行以下 Java 代码,用于从 NSE 证券交易所的 REST API 获取期权链数据。首先,我对主页进行 GET 操作,并使用后续请求中响应中的 cookie 来实际获取选项链数据。我根据计划的任务不断重复这两个步骤。它可以工作一两次,但之后它开始在 HTTP 响应中给出 401 未经授权的错误。我在两个请求标头中设置浏览器名称。任何帮助深表感谢。

import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.io.InputStream;

public class PollNSEIndia {
    public static void main(String args[]) throws Exception {
        while (true) {
            HttpURLConnection baseUrlConnection = (HttpURLConnection) new URL("https://www.nseindia.com/").openConnection();
            baseUrlConnection.setRequestProperty("Connection", "keep-alive");
            baseUrlConnection.setRequestProperty("Cache-Control", "max-age=0");
            baseUrlConnection.setRequestProperty("Upgrade-Insecure-Requests", "1");
            baseUrlConnection.setRequestProperty(
                    "User-Agent",
                    "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)"
                            + " Chrome/89.0.4389.114 Safari/537.36");
            baseUrlConnection.setRequestProperty(
                    "Accept",
                    "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
            baseUrlConnection.setRequestProperty("Accept-Language", "en-US,en;q=0.9");
            List<String> cookies = baseUrlConnection.getHeaderFields().get("Set-Cookie");

            URL url = new URL("https://www.nseindia.com/api/option-chain-indices?symbol=MIDCPNIFTY");
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("GET");
            for (String cookie : cookies) {
                httpURLConnection.addRequestProperty("Cookie", cookie.split(";", 2)[0]);
            }
            httpURLConnection.setRequestProperty("Connection", "keep-alive");
            httpURLConnection.setRequestProperty("Cache-Control", "max-age=0");
            httpURLConnection.setRequestProperty("Upgrade-Insecure-Requests", "1");
            httpURLConnection.setRequestProperty(
                    "User-Agent",
                    "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)"
                            + " Chrome/89.0.4389.114 Safari/537.36");
            httpURLConnection.setRequestProperty(
                    "Accept",
                    "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
            httpURLConnection.setRequestProperty("Accept-Language", "en-US,en;q=0.9");
            InputStream inputStream = httpURLConnection.getInputStream();
            System.out.println("Got inputstream.");
            Thread.sleep(1000);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Ani*_* B. 6

您在调用时添加了多个Cookie请求属性"https://www.nseindia.com/api/option-chain-indices?symbol=MIDCPNIFTY",这导致了 401。

Cookie另外,我更新了您的代码以修复您在执行请求时添加标头的方式。

现在这可以工作了:)

尝试这个:

import com.fasterxml.jackson.databind.ObjectMapper;
import java.net.*;
import java.util.List;
import java.io.InputStream;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

public class PollNSEIndia {
    public static void main(String args[]) throws Exception {
        while (true) {
            CookieManager cookieManager = new CookieManager();
            CookieHandler.setDefault(cookieManager);
            HttpURLConnection baseUrlConnection = (HttpURLConnection) new URL("https://www.nseindia.com/").openConnection();
            baseUrlConnection.setRequestProperty("Connection", "keep-alive");
            baseUrlConnection.setRequestProperty("Cache-Control", "max-age=0");
            baseUrlConnection.setRequestProperty("Upgrade-Insecure-Requests", "1");
            baseUrlConnection.setRequestProperty(
                    "User-Agent",
                    "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)"
                            + " Chrome/89.0.4389.114 Safari/537.36");
            baseUrlConnection.setRequestProperty(
                    "Accept",
                    "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
            baseUrlConnection.setRequestProperty("Accept-Language", "en-US,en;q=0.9");
            baseUrlConnection.getContent();
            List<HttpCookie> cookieList = cookieManager.getCookieStore().getCookies();

            URL url = new URL("https://www.nseindia.com/api/option-chain-indices?symbol=MIDCPNIFTY");
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("GET");
            httpURLConnection.setRequestProperty("Cookie", cookieList.stream().filter(Objects::nonNull)
                    .map(cookie -> cookie.getName() + "=" + cookie.getValue()).collect(Collectors.joining(";")));
            httpURLConnection.setRequestProperty("Connection", "keep-alive");
            httpURLConnection.setRequestProperty("Cache-Control", "max-age=0");
            httpURLConnection.setRequestProperty("Upgrade-Insecure-Requests", "1");
            httpURLConnection.setRequestProperty(
                    "User-Agent",
                    "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)"
                            + " Chrome/89.0.4389.114 Safari/537.36");
            httpURLConnection.setRequestProperty(
                    "Accept",
                    "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9");
            httpURLConnection.setRequestProperty("Accept-Language", "en-US,en;q=0.9");
            InputStream inputStream = httpURLConnection.getInputStream();
            System.out.println(httpURLConnection.getResponseCode());
            System.out.println("Got inputstream.");

            // checking output via Jackson for testing (ignore this)
            ObjectMapper mapper = new ObjectMapper();
            Map data = mapper.readValue(inputStream, Map.class);
            System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(data));

            Thread.sleep(1000);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

输出:

200
Got inputstream.
{
  "records" : {
    "expiryDates" : [ "09-Oct-2023", "16-Oct-2023", "23-Oct-2023", "30-Oct-2023", "06-Nov-2023", "22-Dec-2023" ],
    "data" : [ {
      "strikePrice" : 7800,
      "expiryDate" : "09-Oct-2023",
      "PE" : {
        "strikePrice" : 7800,
        "expiryDate" : "09-Oct-2023",
        "underlying" : "MIDCPNIFTY",
        "identifier" : "OPTIDXMIDCPNIFTY09-10-2023PE7800.00",
        "openInterest" : 386,
        "changeinOpenInterest" : -33,
        "pchangeinOpenInterest" : -7.875894988066825,
        "totalTradedVolume" : 985,
        "impliedVolatility" : 52.73,
        "lastPrice" : 0.05,
        "change" : -0.4,
....
Run Code Online (Sandbox Code Playgroud)