如何在F#中发出HTTP POST请求? [英] How can I make an HTTP POST request in F#?

查看:204
本文介绍了如何在F#中发出HTTP POST请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

The following code sends a GET request. This is making me crazy.

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。有没有人见过这方面的文件?

At the moment I'm not sure that anyone has ever used F# to send an HTTP POST. Has anyone seen documentation about this?

推荐答案

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

Seems to work fine for me. For example (posttestserver.com does really exist):

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.
"

也许你以不同的方式使用它?

Maybe you are using it in a different way?

这篇关于如何在F#中发出HTTP POST请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆