0

I have a requirement to encode the file data into base64 encoding and upload the file, This works completely fine if the file size is less than 2-3KB when i try sending relatively big files it throws 403 error(Remote Server returned an error: (403) Forbidden), There is no issue with authentication it is successfully established.

Below is the code snippet I am using to make request

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

I am not sending the file as multipart/formdata as my requirement needs the file data to be encoded, what change should i make to get this working.

Akshay
  • 407
  • 1
  • 4
  • 14

1 Answers1

0

Try increasing your maxRequestLength and maxAllowedContentLength in the web.config:

  <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?

Tom John
  • 783
  • 5
  • 14
  • I have made these settings in web.config. I am still not able to make requests. – Akshay Sep 20 '17 at 11:07
  • Are you sure it's the length of what you are posting that's the issue or are you trying to post some form of XML or HTML which is being blocked by default? – Tom John Sep 20 '17 at 11:10
  • Yes, I tried to upload a text file of 200bytes it works fine, but when i try to upload 600KB text file it fails. I tried upload posting zip files and xml also with little bigger size, but fails. same works for small files. – Akshay Sep 20 '17 at 11:24
  • Can you add the text file as I have knocked up a basic MVC form and post back which is working fine. – Tom John Sep 20 '17 at 11:37
  • Yes i am using text file, but no luck for 600KB file. – Akshay Sep 20 '17 at 11:46