如何从Rust发出HTTP请求? [英] How do I make an HTTP request from Rust?
问题描述
如何从Rust发出HTTP请求?我似乎无法在核心库中找到任何内容。
我不需要解析输出,只需发出请求并检查HTTP响应代码。 / p>
如果有人可以告诉我如何对我的网址上的查询参数进行URL编码,则可以获得奖励!
更新:这个答案指的是相当古老的历史。有关当前的最佳做法,请查看 Isaac Aggrey的回答。
我一直致力于生锈 - http ,它已经成为Rust的事实上的 HTTP库(Servo使用它);它远非完整,目前记录非常糟糕。以下是使用状态代码发出请求并执行操作的示例:
extern mod http;
使用http :: client :: RequestWriter;
使用http :: method :: Get;
使用http :: status;
使用std :: os;
fn main(){
let request = RequestWriter :: new(Get,FromStr :: from_str(os :: args()[1])。unwrap());
let response = match request.read_response(){
Ok(response)=>响应,
Err(_request)=>无法到达!(),//未捕获的条件将首先失败
};
如果response.status == status :: Ok {
println!(哦,好吧,我给了我一个200 OK响应!);
} else {
println!(该URL未返回200 OK,它返回{}而不是,response.status);
}
}
使用URL作为唯一命令运行此代码 - 行参数,它将检查状态代码! (仅限HTTP;无HTTPS。)
与 src / examples / client / client.rs
比较示例那会多一点。
rust-http正在跟踪生锈的主分支。 目前它将在刚刚发布的Rust 0.8中运行,但很快就会出现重大变化。 实际上,没有版本的rust-http可以在Rust 0.8上运行 - 有一个在发布之前,在隐私规则中无法解决的更改,留下了一些生锈的http取决于extra :: url不可访问。这已经修复了,但它留下了锈-http与Rust 0.8不兼容。
至于查询字符串编码问题,目前应该使用 extra :: url :: Query
(〜[(~str,~str)]>
)。转换的适当函数:
-
extra :: url :: query_from_str
(对不起,目前不能使用它,因为它是私有的。公关即将公开。同时,这个链接实际上不起作用,它只能用于 https://github.com/ mozilla / rust / issues / 7476 。)
How can I make an HTTP request from Rust? I can't seem to find anything in the core library.
I don't need to parse the output, just make a request and check the HTTP response code.
Bonus marks if someone can show me how to URL encode the query parameters on my URL!
Update: This answer refers to fairly ancient history. For the current best practices, please look at Isaac Aggrey's answer instead.
I've been working on rust-http, which has become the de facto HTTP library for Rust (Servo uses it); it's far from complete and very poorly documented at present. Here's an example of making a request and doing something with the status code:
extern mod http;
use http::client::RequestWriter;
use http::method::Get;
use http::status;
use std::os;
fn main() {
let request = RequestWriter::new(Get, FromStr::from_str(os::args()[1]).unwrap());
let response = match request.read_response() {
Ok(response) => response,
Err(_request) => unreachable!(), // Uncaught condition will have failed first
};
if response.status == status::Ok {
println!("Oh goodie, I got me a 200 OK response!");
} else {
println!("That URL ain't returning 200 OK, it returned {} instead", response.status);
}
}
Run this code with a URL as the sole command-line argument and it'll check the status code! (HTTP only; no HTTPS.)
Compare with src/examples/client/client.rs
for an example that does a little more.
rust-http is tracking the master branch of rust. At present it'll work in the just-released Rust 0.8, but there are likely to be breaking changes soon. Actually, no version of rust-http works on Rust 0.8—there was a breaking change which can't be worked around in privacy rules just before the release, leaving something that rust-http depends on in extra::url inaccessible. This has since been fixed, but it leaves rust-http incompatible with Rust 0.8.
As for the query string encoding matter, at present that should be done with extra::url::Query
(a typedef for ~[(~str, ~str)]
). Appropriate functions for conversions:
extra::url::query_from_str
(sorry, can't use this just at present as it's private. PR to make it public about to come. In the mean time, this link actually shouldn't work, it's only available because of https://github.com/mozilla/rust/issues/7476.)
这篇关于如何从Rust发出HTTP请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!