在HTTPPost请求中发送非常大的字符串数据

时间:2017-09-20 09:49:43

标签: asp.net-web-api file-upload http-post

我要求将文件数据编码为base64编码并上传文件,如果文件大小小于2-3KB,当我尝试发送相对较大的文件时,它会发生403错误(远程服务器返回错误:(403)禁止),验证成功建立没有问题。

以下是我用来提出请求的代码段

Public Shared Function PostData() As String
Dim sReturnValue As String = String.Empty
Dim sUrl As String = http://localhost:50562/API/UploadFile/UploadSingleFile
Dim oRequest As HttpWebRequest
Dim oResponse As HttpWebResponse

oRequest = TryCast(WebRequest.Create(sUrl), HttpWebRequest)
oRequest.AllowWriteStreamBuffering = True
oRequest.Method = "POST"
oRequest.ContentType = "text/json"
oRequest.Headers.Add("Accept-Language", "en-us")
If m_bPassKeyInHeader = True Then
    oRequest.Headers.Add("APIKey", m_sAPIKey)
End If
If i_sData IsNot Nothing AndAlso i_sData.Length > 0 Then
    Using oWriter As New StreamWriter(oRequest.GetRequestStream())
        oWriter.Write(i_sData)
    End Using
End If

Try
    oResponse = oRequest.GetResponse()
Catch ex As Exception
End Try

Using oReader As New StreamReader(oResponse.GetResponseStream())
    sReturnValue = oReader.ReadToEnd()
End Using

Return sReturnValue

我不是将文件作为multipart / formdata发送,因为我的要求需要编码的文件数据,我应该做些什么改变才能使其正常工作。

1 个答案:

答案 0 :(得分:0)

尝试在web.config中增加maxRequestLength和maxAllowedContentLength:

  <system.web>
    <httpRuntime targetFramework="4.6.2" maxRequestLength="1048576" />
  </system.web>

...

  <system.webServer>
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="1073741824" />
      </requestFiltering>
    </security>
  </system.webServer>

Which gets priority, maxRequestLength or maxAllowedContentLength?