gpt4 book ai didi

http - 如何在 F# 中发出 HTTP POST 请求?

转载 作者:可可西里 更新时间:2023-11-01 15:29:21 29 4
gpt4 key购买 nike

以下代码发送 GET 请求。这让我发疯。

let postDocRaw (url:string) (data: string) : string =
let data' : byte[] = System.Text.Encoding.ASCII.GetBytes(data);

let request = WebRequest.Create(url)
request.Method <- "POST"
request.ContentType <- "application/x-www-form-urlencoded"
request.ContentLength <- (int64) data'.Length

use wstream = request.GetRequestStream()
wstream.Write(data',0, (data'.Length))
wstream.Flush()
wstream.Close()
(*
use writer = new StreamWriter (wstream)
writer.Write(data')
writer.Flush()
writer.Close()
*)

let response = request.GetResponse()
use reader = new StreamReader(response.GetResponseStream())
let output = reader.ReadToEnd()

reader.Close()
response.Close()
request.Abort()

output

目前我不确定是否有人使用过 F# 发送 HTTP POST。有没有人看过这方面的文档?

最佳答案

似乎对我来说工作得很好。例如(posttestserver.com 确实存在):

printfn "response: %A" (postDocRaw "http://posttestserver.com/post.php" "hello=data")
Console.ReadLine() |> ignore

结果:

response: "
Successfully dumped 1 post variables.Post body was 0 chars long.
"

也许您正在以不同的方式使用它?

关于http - 如何在 F# 中发出 HTTP POST 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5454157/

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