我可以使用httprequest自动发布文件,就像我定期上传一样吗?

时间:2013-08-05 14:39:12

标签: post file-io asp-classic multipartform-data

我坚持认为(我认为)应该很简单,但我无法弄清楚如何做到这一点。我在SO或Google上找不到任何信息。

我们有一个网络应用程序从我们的网站获取数据并自动将其发送到另一个API。

我们也希望通过二进制文件发送 - 例如,我们从本地URL获取文件并将其内容发布到其他站点。

有人能指出我正确的方向吗?例如,我是否需要以某种方式将文件URL嵌入数据中并将POST作为multipart / form-data?

1 个答案:

答案 0 :(得分:1)

我创建了这些经典ASP函数来将文件POST到另一台服务器。希望它也能帮到你。

Function PostDocument(intDocumentID, binFile, strFilename, strContentType)
Dim objHttp, strBoundary, strRequestStart, strRequestEnd, binPost
Dim objStream

strBoundary = "---------------------------9849436581144108930470211272"

Set objHttp = Server.CreateObject("MSXML2.ServerXMLHTTP.6.0")

strRequestStart = "--" & strBoundary & vbCrlf &_
    "Content-Disposition: form-data; name=""id""" & vbCrlf &_
    vbCrlf &_
    intDocumentID & vbCrlf &_
    vbCrlf &_
    "--" & strBoundary & vbCrlf &_
    "Content-Disposition: form-data; name=""file""; filename=""" & strFilename & """" & vbCrlf &_
    "Content-Type: " & strContentType & vbCrlf &_
    vbCrlf

strRequestEnd = vbCrLf & "--" & strBoundary & "--"

Set objStream = Server.CreateObject("ADODB.Stream")

objStream.Type = adTypeBinary '1
objStream.Mode = adModeReadWrite '3
objStream.Open
objStream.Write StringToBinary(strRequestStart)
objStream.Write binFile
objStream.Write StringToBinary(strRequestEnd)
objStream.Position = 0

binPost = objStream.Read

Response.Write binPost

objStream.Close
Set objStream = Nothing

objHttp.Open "POST", "(url removed)", False, "(username removed)", "(password removed)"
objHttp.setRequestHeader "Content-Type", "multipart/form-data; boundary=""" & strBoundary & """"
objHttp.Send binPost

PostDocument = objHttp.ResponseText

Set objHttp = Nothing
End Function

Function StringToBinary(toConvert)
Dim objStream, data

Set objStream = Server.CreateObject("ADODB.Stream")

objStream.Charset = "ISO-8859-1"
objStream.Type = adTypeText '2
objStream.Mode = adModeReadWrite '3
objStream.Open
objStream.WriteText toConvert

objStream.Position = 0
objStream.Type = adTypeBinary '1
StringToBinary = objStream.Read

objStream.Close
Set objStream = Nothing
End Function