ASP.net Error posting data to API

Murray Brown
Murray Brown used Ask the Experts™
on
Hi

In my ASP.net web app.  I am editing data via an API and sometimes
get the error I the image. My code is as follows. I have no idea what this error means

 Public Function PostEmployeeHoursEdit(DataLines As List(Of LineEdit), StoreID As Integer) As String

        Dim Resp As HttpWebResponse
        Try
            Dim url As String = "http://www.rxxdf.com/platform/api/"
            Dim API_Method As String = "EmployeeHours"

            Dim data As String = ""
            data = Newtonsoft.Json.JsonConvert.SerializeObject(DataLines)

            Dim Auth As String = Me.txtUsername.Text & ":" & Me.txtPassword.Text
            Dim webAddress As String = url & API_Method & "/" & StoreID & "/0"
            Dim request As HttpWebRequest = HttpWebRequest.Create(webAddress)
            request.Method = WebRequestMethods.Http.Post
            request.Headers.Add("Authorization", Auth)
            request.ContentType = "application/json"
            request.ContentLength = data.Length

            Dim writer As New StreamWriter(request.GetRequestStream(), System.Text.Encoding.ASCII)
            writer.Write(data)
            writer.Close()


            Resp = request.GetResponse()
            Dim stream As Stream = request.GetResponse().GetResponseStream()
            Dim reader As New StreamReader(stream)
            Dim response As String = reader.ReadToEnd()

            Return "OK"

        Catch wex As WebException
            Dim response As WebResponse = wex.Response
            Dim statusCode As HttpStatusCode

            Dim httpResponse As HttpWebResponse = CType(response, HttpWebResponse)
            statusCode = httpResponse.StatusCode
            Return statusCode & " " & httpResponse.StatusDescription
        End Try

    End Function

Open in new window


1
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Add this to Configuration section in web.config:

    <system.web.extensions>
          <scripting>
                  <webServices>
                      <jsonSerialization maxJsonLength="67000000"/>
                     </webServices>
           </scripting>
</system.web.extensions>

If error persist then change :
Writer.Write(data)
To write data in blocks of 8k
Murray BrownASP.net/VSTO Developer

Author

Commented:
Thanks very much

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial