rookie0 / rains

💹 命令行 A 股沪深北证股票信息行情数据查询工具
MIT License
100 stars 14 forks source link

panicked at 'called `Result::unwrap()` on an `Err` value: Http(Response { status: 403, version: HTTP/1.1, headers: {"content-length": "9"}, body: Some([70, 111, 114, 98, 105, 100, 100, 101, 110]) })', #4

Open lucasjinreal opened 1 year ago

lucasjinreal commented 1 year ago
panicked at 'called `Result::unwrap()` on an `Err` value: Http(Response { status: 403, version: HTTP/1.1, headers: {"content-length": "9"}, body: Some([70, 111, 114, 98, 105, 100, 100, 101, 110]) })',

貌似最新的rust版本,不这么指定回报header错误,制定了也会报错:

/// 多个时连接时返回所有 之后单个返回
    pub async fn quotes_ws(symbols: &str, handler: impl Fn(Vec<Quote>)) {
        let req = Request::builder()
            .method(Method::GET)
            .uri(format!(
                "wss://hq.sinajs.cn/wskt?list={}",
                fmt_quote_symbols(symbols)
            ))
            .header(header::ORIGIN, HeaderValue::from_static(PORTAL))
            .header(header::CONNECTION, "Upgrade")
            .header(header::UPGRADE, "websocket")
            .header("Sec-WebSocket-Version", "13")
            .header(header::HOST, HeaderValue::from_static(PORTAL))
            .header("Sec-WebSocket-Key", tungstenite::handshake::client::generate_key())
            .body(())
            .unwrap();

Any idea?

rookie0 commented 1 year ago

给下具体 rust 版本及不指定报的错误,我用 1.69.0-nightly 本地编译测试没问题

指定报错 403 跟这些加的 header 没关系

lucasjinreal commented 1 year ago

@rookie0 很奇怪,我换了两台电脑了,都是i这个问题,而且是runtime panic, 就是在这个方法内,其他都无问题

 rustc --version
rustc 1.67.0 (fc594f156 2023-01-24)
 panicked at 'called `Result::unwrap()` on an `Err` value: Protocol(InvalidHeader("sec-websocket-key"))'

很奇怪啊,我刚才编译了一下git代码,为啥有时候他会报这个错,有时候不会?

我加了一下header,然后就是403的错误:

  // let req = Request::builder()
        //     .method(Method::GET)
        //     .uri(format!(
        //         "wss://hq.sinajs.cn/wskt?list={}",
        //         fmt_quote_symbols(symbols)
        //     ))
        //     // .header(header::ORIGIN, HeaderValue::from_static(PORTAL))
        //     .header(header::CONNECTION, "Upgrade")
        //     .header(header::UPGRADE, "websocket")
        //     .header("Sec-WebSocket-Version", "13")
        //     .header(header::HOST, HeaderValue::from_static(PORTAL))
        //     .header("Sec-WebSocket-Key", tungstenite::handshake::client::generate_key())
        //     .body(())
        //     .unwrap();

        let req = Request::builder()
            .method(Method::GET)
            .uri(format!(
                "wss://hq.sinajs.cn/wskt?list={}",
                fmt_quote_symbols(symbols)
            ))
            .header(header::ORIGIN, HeaderValue::from_static(PORTAL))
            .body(())
            .unwrap();

是我缺少了什么配置么

rookie0 commented 1 year ago

403 是因为用的 header 不对,应该是 origin

InvalidHeader("sec-websocket-key") 的问题 macos 未复现,不确定是否是 tungstenite 这个库不支持 windows 平台,你可以从这方面跟下

image

lucasjinreal commented 1 year ago

@rookie0 我mac下测试了也同样报错。 rust 是stable的最新版。

lucasjinreal commented 1 year ago

btw, 我是mac M1 pro

lucasjinreal commented 1 year ago

@rookie0 你clone一个git的不更改版本,编译一下看看么,我在公司电脑编译你的代码没有问题(windows), 在家用windows mac都会又那个runtime panic,缺少header。

是因为vpn?》??

这个问题让人丈二和尚摸不到头脑啊,