connection error with webserver RRS feed

  • Question

  • hello again 
    i need your help 
    i made a simple app that calls a webmethod and gets a json string as response 
    everything worked fine up untill yesterday when suddenlly it stop working for no reason 
    when the code tries to get the response the message i get is 
    "A first chance exception of type 'System.Net.WebException' occurred in System.dll"

    what i dont understand is why did it stop working in both the form app i made and a dll that i use to a different project the code and libraries used in both of them are exacly the same and no change was made for the last 3 months 
     here is the code 

    Imports System.Net
    Imports System.IO
    Imports System.Text
    Imports Newtonsoft.Json
    Imports System.Web
    Public Class Form1
        Public Class Get_all_closed_bills_class
            Property ClientToken As String
            Property AccessToken As String
            Property StartUtc As String
            Property EndUtc As String
        End Class
        Public Function Get_all_closed_bills(Request As String,
                                            Username As String,
                                            Password As String,
                                            AccessToken As String,
                                            ClientToken As String,
                                            StartUtc As String,
                                            EndUtc As String)
            Dim strRequest As String = Request
            Dim strJsonData As String
            Dim objhttpWebRequest As HttpWebRequest
            Dim obj_Get_all_closed_bills As New Get_all_closed_bills_class
            obj_Get_all_closed_bills.ClientToken = ClientToken
            obj_Get_all_closed_bills.AccessToken = AccessToken
            obj_Get_all_closed_bills.StartUtc = StartUtc
            obj_Get_all_closed_bills.EndUtc = EndUtc
            strJsonData = JsonConvert.SerializeObject(obj_Get_all_closed_bills, Newtonsoft.Json.Formatting.Indented)
                Dim MyCredentials As System.Net.NetworkCredential
                MyCredentials = New System.Net.NetworkCredential(Username, Password)
                Dim httpWebRequest = DirectCast(WebRequest.Create(strRequest), HttpWebRequest)
                httpWebRequest.Credentials = MyCredentials
                httpWebRequest.ContentType = "application/json"
                httpWebRequest.Method = "POST"
                Using streamWriter = New StreamWriter(httpWebRequest.GetRequestStream())
                End Using
                objhttpWebRequest = httpWebRequest
            Catch ex As Exception
                Console.WriteLine("Send Request Error[{0}]", ex.Message)
                Return Nothing
            End Try
            Return GetResponse(objhttpWebRequest)
        End Function
        Private Function GetResponse(ByVal httpWebRequest As HttpWebRequest) As String
            Dim strResponse As String = "Bad Request:400"
                Dim httpResponse = DirectCast(httpWebRequest.GetResponse(), HttpWebResponse)
                Using streamReader = New StreamReader(httpResponse.GetResponseStream())
                    Dim result = streamReader.ReadToEnd()
                    strResponse = result.ToString()
                End Using
            Catch ex As Exception
                Console.WriteLine("GetResponse Error[{0}]", ex.Message)
                Return ex.Message
            End Try
            Return strResponse
        End Function
        Private Sub btnRetrieve_Click(sender As System.Object, e As System.EventArgs) Handles btnRetrieve.Click
            Dim strResponse As String
            ' request 
            ' username
            ' password
            ' AccessToken
            ' ClientToken
            ' StartUtc
            ' EndUtc
            'strResponse = 
            Dim start_date As String = Format(StartDate.Value, "yyyy-MM-ddThh:mm:ss.00Z")
            Dim end_date As String = Format(EndDate.Value, "yyyy-MM-ddThh:mm:ss.00Z")
            strResponse = Get_all_closed_bills("", _
                                                             "", _
                                                             "connector-api", _
                                                             "C66EF7B239D24632943D115EDE9CB810-EA00F8FD8294692C940F6B5A8F9453D", _
                                                                "E0D439EE522F44368DC78E1BFB03710C-D24FB11DBE31D4621C4817E028D9E1D", _
                                                                start_date, _
            TextBox1.Text = strResponse
        End Sub
    End Class

    as soon as the code get to  :
      Dim httpResponse = DirectCast(httpWebRequest.GetResponse(), HttpWebResponse)

    i get the "A first chance exception of type 'System.Net.WebException' occurred in System.dll" error 
    Tuesday, September 4, 2018 9:49 AM

All replies

  • Hi Piptabor,

    From your description, I am not clear about what is your project type. WCF or web service? and where does the problem lies? If the error occurred during the calling of Web service. I suggest you could use the postman to send a request to check whether it is related to the httpwebrequest, try to add a breakpoint to debug the service and confirm whether there is an error on server-side.

    Here is a blog about the error details.

    Feel free to let me know if you have any questions.

    Best Regards

    Thursday, September 6, 2018 3:11 AM
  • Well 

    it was 2 different projects 

    a dll that have the calling methods and a windows form app both created in visual studio 2010 at the same computer 

    and they both worked fine 

    when suddenly both of of them stoped working. i tried the form app to another computer and it also didn't worked 

    i took the code and recreated the dll and form to VS 2017 and it works like a charm again i am starting to think it is something about the 4.0 net framework of vs 2010 

    Monday, September 10, 2018 7:16 AM
  • Hi Lpodamia,

    From your description,

    >>i took the code and recreated the dll and form to VS 2017 and it works like a charm again i am starting to think it is something about the 4.0 net framework of vs 2010 

    If the project’s target framework is related to this issue, I recommend that you could create an application with target .net framework4.0 in VS2017 and check if you could reproduce the previous problem (since VS2010 supports the target .net framework4.0).  if it could reproduce the problem, it could be determined that there is something wrong in the compatibility of the project’s target framework.

    Best Regards

    Thursday, September 13, 2018 2:38 AM