通过Webrequest发布表单数据并上传文件

时间:2018-11-07 10:56:08

标签: asp.net vb.net

我需要上传一个带有webrequest的文件,并将其与请求中的其他表单数据合并。今天我的帖子请求看起来像这样:

 Protected Function PostData(ByRef url As String, ByRef POST As String, ByRef Cookie As System.Net.CookieContainer) As String
    Dim request As HttpWebRequest
    Dim response As HttpWebResponse
    request = CType(WebRequest.Create(url), HttpWebRequest)
    request.ContentType = "application/x-www-form-urlencoded"
    request.ContentLength = POST.Length
    request.Method = "POST"
    request.AllowAutoRedirect = False

    Dim requestStream As Stream = request.GetRequestStream()
    Dim postBytes As Byte() = Encoding.ASCII.GetBytes(POST)
    requestStream.Write(postBytes, 0, postBytes.Length)
    requestStream.Close()
    response = CType(request.GetResponse, HttpWebResponse)
    Return New StreamReader(response.GetResponseStream()).ReadToEnd()
End Function

字符串发布(位于request.ContentLength中)现在包含以下表单数据:

"access_token=xxyyzzz"

现在我还要添加此表单数据参数: 文件(要上传的pdf文件) receiver_email(字符串值) receive_name(字符串值)

我需要如何更改PostData函数才能将文件和字符串数据都发布到webrequest中?

感谢帮助!

彼得

0 个答案:

没有答案