Foxtable(狐表)用户栏目专家坐堂 → [求助]如何用post方法替换API中的图片地址?


  共有7590人关注过本帖树形打印复制链接

主题:[求助]如何用post方法替换API中的图片地址?

帅哥哟,离线,有人找我吗?
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:三尾狐 帖子:732 积分:5491 威望:0 精华:14 注册:2011/8/28 12:49:00
  发帖心情 Post By:2013/9/6 11:05:00 [显示全部帖子]

    '正好前段时间开发一个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编辑过]

 回到顶部
帅哥哟,离线,有人找我吗?
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:三尾狐 帖子:732 积分:5491 威望:0 精华:14 注册:2011/8/28 12:49:00
  发帖心情 Post By:2013/9/6 11:37:00 [显示全部帖子]

你不如在自己的服务器上放图片,然后用get把图片的url发送给它,然后让它自己处理,post比较麻烦一点

 回到顶部