vb.net用文件创建API请求 [英] vb.net create API request with file

查看:92
本文介绍了vb.net用文件创建API请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿!



我希望有人能帮助我:

我想用WebRequest调用API并发送一个文件(存储在服务器上)



Dim request As HttpWebRequest

Dim response As HttpWebResponse = Nothing

昏暗读者作为StreamReader

昏暗地址As Uri

Dim byteData()As Byte

Dim reader0作为StreamReader



Dim gui As String = Guid.NewGuid()。ToString()

Dim encoding As String =iso-8859-1

Dim head As String = String.Format(--------------------------------,gui)

Dim foot As String = String.Format(-------------------------------- ,gui)

Dim contents As StringBuilder = New StringBuilder()

contents.AppendLine(head)

contents.AppendLine(String.Format (Content-Disposition:form-data; name =文件[] ; filename =HELLO.pdf))



Dim fInfo作为新FileInfo(Server.MapPath(〜/ HELLO.pdf))

Dim numBytes As Long = fInfo.Length

Dim fStream As New FileStream(Server.MapPath(〜/ HELLO.pdf),FileMode.Open,FileAccess.Read)

Dim br As New BinaryReader(fStream)

Dim data As Byte()= br.ReadBytes(CInt(numBytes))

br.Close( )

fStream.Close()

contents.AppendLine(String.Format(Content-Type:application / pdf))

内容.AppendLine()

contents.AppendLine(data.ToString)

contents.AppendLine()

contents.AppendLine(foot)

contents.AppendLine(String.Format(Content-Disposition:form-data; name =recipients []))

contents.AppendLine()

contents.AppendLine(name@example.com)

contents.AppendLine()

contents.AppendLine(foot& - )



address = New Uri(http://api.example.com/signs.json)

request = DirectCast(WebRequest.Create(address),HttpWebRequest)

request.Method =POST

request.Headers(Authorization) =Bearer YzM0OGY4NThlMWM0YjZlNWFmNjkzNzg4Y2ViMTdkZmYxMadfsdafdafmEzYWU5N2VlNWExNA

request.ContentType = String.Format(multipart / form-data; boundary = {0},gui)

Dim bytes As Byte()= System.Text.Encoding.GetEncoding(encoding).GetBytes(contents.ToString())

request.ContentLength = bytes.Length

request.Host =api.example.com



尝试

Dim NewStream As Stream = request.GetRequestStream

NewStream.Write(bytes,0,bytes.Length)

'获取回复

回复= request.GetResponse()





'将响应流输入读卡器

reader0 =新StreamReader (response.GetResponseStream())

ViewBag.test = reader0.ReadToEnd()

reader0.Close()

Catch ex As WebException

ViewBag.test = ex.StackTrace

结束尝试



当我使用此代码时,没有收到任何信息服务器



我做错了什么?

非常感谢!!!!

干杯

Hey!

I hope someone of you can help me with this:
I would like to call a API with a WebRequest and send a file (which is stored on the server)

Dim request As HttpWebRequest
Dim response As HttpWebResponse = Nothing
Dim reader As StreamReader
Dim address As Uri
Dim byteData() As Byte
Dim reader0 As StreamReader

Dim gui As String = Guid.NewGuid().ToString()
Dim encoding As String = "iso-8859-1"
Dim head As String = String.Format("--------------------------------", gui)
Dim foot As String = String.Format("--------------------------------", gui)
Dim contents As StringBuilder = New StringBuilder()
contents.AppendLine(head)
contents.AppendLine(String.Format("Content-Disposition: form-data; name=""files[]""; filename=""HELLO.pdf"""))

Dim fInfo As New FileInfo(Server.MapPath("~/HELLO.pdf"))
Dim numBytes As Long = fInfo.Length
Dim fStream As New FileStream(Server.MapPath("~/HELLO.pdf"), FileMode.Open, FileAccess.Read)
Dim br As New BinaryReader(fStream)
Dim data As Byte() = br.ReadBytes(CInt(numBytes))
br.Close()
fStream.Close()
contents.AppendLine(String.Format("Content-Type: application/pdf"))
contents.AppendLine()
contents.AppendLine(data.ToString)
contents.AppendLine()
contents.AppendLine(foot)
contents.AppendLine(String.Format("Content-Disposition: form-data; name=""recipients[]"""))
contents.AppendLine()
contents.AppendLine("name@example.com")
contents.AppendLine()
contents.AppendLine(foot & "--")

address = New Uri("http://api.example.com/signs.json")
request = DirectCast(WebRequest.Create(address), HttpWebRequest)
request.Method = "POST"
request.Headers("Authorization") = "Bearer YzM0OGY4NThlMWM0YjZlNWFmNjkzNzg4Y2ViMTdkZmYxMadfsdafdafmEzYWU5N2VlNWExNA"
request.ContentType = String.Format("multipart/form-data; boundary={0}", gui)
Dim bytes As Byte() = System.Text.Encoding.GetEncoding(encoding).GetBytes(contents.ToString())
request.ContentLength = bytes.Length
request.Host = "api.example.com"

Try
Dim NewStream As Stream = request.GetRequestStream
NewStream.Write(bytes, 0, bytes.Length)
' Get response
response = request.GetResponse()


' Get the response stream into a reader
reader0 = New StreamReader(response.GetResponseStream())
ViewBag.test = reader0.ReadToEnd()
reader0.Close()
Catch ex As WebException
ViewBag.test = ex.StackTrace
End Try

When I am using this code nothing received at the server

What I am doing wrong?
Thanks a lot!!!!
Cheers

推荐答案

这篇关于vb.net用文件创建API请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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