'正好前段时间开发一个IOS的APP后台接口,给你一段代码吧
'客户端HttpWebPost上传文件代码,在Visual studio 2010中写的,要放进Foxtable需要加一下命名空间
Public Function HttpPostFile(ByVal url As String,
ByVal timeOut As Integer,
ByVal dicFile As Dictionary(Of String, String))
Dim responseContent As String
Dim memStream As New MemoryStream
Dim webRequest As HttpWebRequest = HttpWebRequest.Create(url)
'边界符
Dim boundary As String = "---------------" + DateTime.Now.Ticks.ToString("x")
Dim beginBoundary As Byte() = Encoding.ASCII.GetBytes("--" & boundary & vbCrLf)
Dim endBoundary As Byte() = Encoding.ASCII.GetBytes(vbCrLf & "--" & boundary & "--")
Dim fileStream As FileStream = New FileStream(dicFile("img1"), FileMode.Open, FileAccess.Read)
'设置属性
webRequest.Method = "POST"
webRequest.Timeout = timeOut
webRequest.C & boundary
memStream.Write(beginBoundary, 0, beginBoundary.Length)
'写入文件1
Dim header As String = "Content-Disposition: form-data; name=""img1"";filename=""" & FileIO.FileSystem.GetName(dicFile("img1")) & """" & vbCrLf &
"Content-Type: application/-image" & vbCrLf & vbCrLf
Dim headerbytes As Byte() = Encoding.UTF8.GetBytes(header)
memStream.Write(headerbytes, 0, headerbytes.Length)
Dim buffer(1024) As Byte
Dim bytesRead As Integer = fileStream.Read(buffer, 0, buffer.Length)
While bytesRead > 0
memStream.Write(buffer, 0, bytesRead)
bytesRead = fileStream.Read(buffer, 0, buffer.Length)
End While
memStream.Write(Encoding.ASCII.GetBytes(vbCrLf & "--" & boundary & vbCrLf), 0, beginBoundary.Length)
'写入文件2
header = vbCrLf & "Content-Disposition: form-data; name=""img2"";filename=""" & FileIO.FileSystem.GetName(dicFile("img2")) & """" & vbCrLf &
"Content-Type: application/-image" & vbCrLf & vbCrLf
headerbytes = Encoding.UTF8.GetBytes(header)
memStream.Write(headerbytes, 0, headerbytes.Length)
fileStream = New FileStream(dicFile("img1"), FileMode.Open, FileAccess.Read)
bytesRead = fileStream.Read(buffer, 0, buffer.Length)
While bytesRead > 0
memStream.Write(buffer, 0, bytesRead)
bytesRead = fileStream.Read(buffer, 0, buffer.Length)
End While
'写入最后的结束边界符
memStream.Write(endBoundary, 0, endBoundary.Length)
webRequest.ContentLength = memStream.Length
Dim requestStream As Stream = webRequest.GetRequestStream
memStream.Position = 0
Dim tempBuffer(memStream.Length - 1) As Byte
memStream.Read(tempBuffer, 0, tempBuffer.Length)
memStream.Close()
requestStream.Write(tempBuffer, 0, tempBuffer.Length)
requestStream.Close()
Dim httpWebResponse As WebResponse = webRequest.GetResponse
Using httpStreamReader As New StreamReader(httpWebResponse.GetResponseStream, Encoding.GetEncoding("utf-8"))
responseContent = httpStreamReader.ReadToEnd
End Using
fileStream.Close()
httpWebResponse.Close()
webRequest.Abort()
Return responseContent
End Function
'调用方法
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim token As String = "aaf3308d-8205-4415-9d62-4a27763cc5b2"
Dim url As String = "http://........../CustomerMobile.asmx"
Dim dicFile As New Dictionary(Of String, String)
dicFile.Add("img1", "d:\aaa.xls")
dicFile.Add("img2", "d:\yumtown.ipa")
MsgBox(HttpPostFile(url & "/SubmitEnquiry?user_id=KH/222&uploadfile=1&enquiry_id=XP-1307013&token=" & token,
10000, dicFile))
dicFile = Nothing
End Sub
[此贴子已经被作者于2013-9-6 11:12:03编辑过]