My web client error:Content-Length or Chunked Encoding cannot be set for an operation


May 23, 2012
I am trying to learn how to write a web client. I wrote a WCF web client to the same site in the code below and that worked. I would also like to write one similar to the code below. Any ideas on what I am doing wrong would be great. I am getting the error:
InnerException = {"Content-Length or Chunked Encoding cannot be set for an operation that does not write data."}
Visual Basic:
Public Class Form1
Dim manualWebClient As New System.Net.WebClient()
   Private Sub Jack() Handles Button1.Click
        manualWebClient.Headers.Add("Content-Type", "text/xml;  charset=utf-8")
        manualWebClient.BaseAddress = ""
        Dim reqstr As String = "<s11:Envelope xmlns:s11="""">" & System.Environment.NewLine & _
            "<s11:Body>" & System.Environment.NewLine & _
            "<ns1:GetCityForecastByZIP xmlns:ns1="""">" & System.Environment.NewLine & _
            "<ns1:ZIP>33569</ns1:ZIP>" & System.Environment.NewLine & _
            "</ns1:GetCityForecastByZIP>" & System.Environment.NewLine & _
            "</s11:Body>" & System.Environment.NewLine & _
        ' manualWebClient.Headers.Add("Content-Length", Len(reqstr).ToString)
        Dim bytArguments As Byte() = System.Text.Encoding.ASCII.GetBytes(reqstr)
            Dim bytRetData As Byte() = manualWebClient.UploadData("/WeatherWS/Weather.asmx", "POST", bytArguments)
        Catch ex As Exception
            Dim v As String = ex.ToString
        End Try

    End Sub
End Class
Last edited by a moderator:
Top Bottom