Kelly's profileKelly's ChroniclesPhotosBlogListsMore Tools Help

Kelly's Chronicles

The life and times of a .NET Geek

Search My Blog

Page Views

web analytics
June 02

Kelly Martens: Troubled Times

Due to some recent tragic events, Kelly Martens will be unable to update this blog for an undetermined amount of time in the foreseable future.  If you would like more details, please send a message to Kelly on Windows Live or Facebook or comment here.  I will let him know you inquired, and if he confirms it is ok to share information with you, I will get in touch with you.  In any case, please pray for Kelly as this is one of the darkest periods of his life.  Thank you.
May 05

POST and HTTP Protocol Violation Resolution in vb.net

Good Morning! My daughter played volleyball last night and was once again outstanding. MY eldest daughter has been released. Unfortunately that has not gone so well. She is moving out of our house no later than next Monday and to be honest I am ok with that.

Today’s topic is one that has plagued me for a while and isn’t really a coded solution but a hack. I thought I would share it in case some of you run into the same problem. Basically we want to POST data to the server, and the server saves the file and its response is a tar archive. Making the request works fine. Where I ran into trouble was the saving of the binary response to a variable and eventually a file. First some of the code to give you an idea of what happened:


Join me on Facebook

' Download a single binary file from a server and save it to a local folder
    Public Sub DownloadAndSaveFile(ByVal Url As String, ByVal Filename As String, ByVal User As String, ByVal Pass As String, ByVal Debug As Integer)

        ' The post data template
        Dim PostdataTemplate As String = _
        "handler=SOME_URLENCODED_DATA"

        Dim PostdataArray As Byte() = Encoding.ASCII.GetBytes(Postdata)

        ' Create a new NetworkCredential object
        Dim NetworkCredential As New NetworkCredential(User, Pass)

        Try
            ' Create a new WebClient instance
            Dim myWebClient As New WebClient

            ' Set Preauthenticate property to true
            'myWebClient.PreAuthenticate = True

            ' Associate the NetworkCrbedential object with the 'WebRequest' object
            myWebClient.Credentials = NetworkCredential

            ' Add required HTTP headers to request
            myWebClient.Headers.Add("Accept", "*/*")
            myWebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded")

            ' UploadData method implicitly sets HTTP POST as the request method
            Dim responseArray As Byte() = myWebClient.UploadData(Url, PostdataArray)
            ' The response array as byte generates an exception!

        Catch Ex As Exception
            WriteLine("Error: " & Ex.Message)

        End Try
    End Sub

I get an exception when saving the response as a byte:

Error: The underlying connection was closed: The server committed an HTTP protocol violation.

The problem was that the .NET Framework detected the server did not comply with HTTP 1.1 RFC. This problem may occur when the response contains incorrect headers or incorrect header delimiters.

 

S0 what to do? I don’t have control of the production server so I can’t fix it on that end. So here is where the hack came in. I modified the app.exe.config file in the following way. You can also modify the machine.config file this way but don’t do that.I should note that I know that the server inserting a header with no name or value is against RFC2616.  But I don't have the ability to modify the server's response in the production environment. Make it a great day! 

------------------------------------------------------------------
<configuration>
<system.net>
<settings>
<httpWebRequest useUnsafeHeaderParsing="true" />
</settings>
</system.net>
</configuration>
------------------------------------------------------------------

 

web analytics

Add to Technorati Favorites

Technorati Tags: ,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,
,,,,,,,,,,,,
,,,,,,,,,,,,,,,
April 29

Refresh Treeview From Another Form with vb.net

Good Morning! Well Brett Favre is at it again. He has requested his release from the New York Jets but says he has no intentions of returning “at this time”. Is there any doubt he will be in a Vikings uniform once the season starts? I have just had it with him. The Vikings can have him.

Today’s topic was requested last night. Basically the user wants to update the treeview on another form after the data is updated. Its not complicated. Basically we add an event handler to the original form that handles the update. On the calling form we call that event when the update occurs. Make it a great day!


Join me on Facebook

 

Private Sub CallingForm_AfterUpdate(ByVal Sender As CallingForm, ByVal Item As Object)

        Dim nd As TreeNode

        nd = Me.SearchNodesForHierarchyItem(Me.mytreeview.Nodes, Item.ID)

        If Not nd Is Nothing Then
            nd.Text = Item.Name
            nd.EnsureVisible()
            Me.mytreeview.SelectedNode = nd
        Else
            Me.Refresh()
            nd = Me.SearchNodesForHierarchyItem(Me.mytreeview.Nodes, Item.ID)
            If Not nd Is Nothing Then
                nd.EnsureVisible()
                Me.mytreeview.SelectedNode = nd
            End If
        End If

    End Sub

Private Sub Refresh()

    Try

      Me.mytreeview.Nodes.Clear()

      If Me.cboTree.SelectedKey = 0 Then

        For Each h As Hierarchy In Hierarchy.TMHierarchies

          Dim n As New HierarchyNode(h.Name)

          n.ImageIndex = 0
          n.SelectedImageIndex = n.ImageIndex
          n.Tag = h
          mytreeview.Nodes.Add(n)

          If h.Children.Count > 0 Then
            n.Nodes.Add(New HierarchyItemNode)
          End If

          n.Expand()
        Next

      Else

        Dim h As Hierarchy = New Hierarchy(CInt(Me.cboTree.SelectedKey))

        Dim n As New HierarchyNode(h.Name)

        n.ImageIndex = 0
        n.SelectedImageIndex = n.ImageIndex
        n.Tag = h
        mytreeview.Nodes.Add(n)

        If h.Children.Count > 0 Then
          n.Nodes.Add(New HierarchyItemNode)
        End If

        n.Expand()

      End If

      If Me.mytreeview.GetNodeCount(False) > 0 Then Me.mytreeview.SelectedNode = Me.mytreeview.Nodes(0)

    Catch ex As Exception

    End Try

  End Sub

 

and on the calling form simly call after an update:

Public Event AfterUpdate(ByVal Sender As CallingForm, ByVal Item As Object)

web analytics

Add to Technorati Favorites

Technorati Tags: ,,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,
,,
April 27

Scroll Control Into View In Panel with vb.net

Good Morning! This weekend was pretty busy. In addition to my side work and work on my current projects we had meetings with people about the formation of another band, and making plans to do some work on my house this summer.

Today’s topic was asked of me and it can be a little frustrating I guess. Basically we want to scroll within a panel to a particular control within that panel and have that control come into view. Not complicated but it works. Make it a great day!


Join me on Facebook

Public Class Form1
    Public Class MyData
        Public Sub New(ByVal ctl As Control)
            Me.Ctl = ctl
        End Sub
        Public Ctl As Control
        Public Overrides Function ToString() As String
            Return Ctl.Name
        End Function
    End Class
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For Each ctl As Control In Panel1.Controls
            If TypeOf ctl Is GroupBox Then
                ComboBox1.Items.Add(New MyData(ctl))
            End If
        Next
    End Sub
    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        Dim ctl As Control = DirectCast(ComboBox1.SelectedItem, MyData).Ctl
        Panel1.ScrollControlIntoView(ctl)
    End Sub
End Classweb analytics

Add to Technorati Favorites

Technorati Tags: ,,,,,,,,,,,,,,,,,,
,,

April 23

Format XML with vb.net

Good Morning! Hope all is well in your neck of the woods. I play bass for a band tonight as well as hopefully getting to catch my daughter in a volleyball game. So today’s topic is how to format xml with vb.net. When I was grabbing and displaying the XML string, it came without line breaks and such which made it difficult to read. So my solution is here. It isn’t elegant or cute but it does work. Make it a great day!


Join me on Facebook

Private Function ParseXml(ByVal xml As String) As String
        Dim builder As New System.Text.StringBuilder()
        Dim xmlString As String = xml.Replace(">", ">" & vbCrLf). _
                Replace("</", vbCrLf & "</").Replace(vbCrLf & vbCrLf, vbCrLf).Trim()
        Dim xmlStringArray As String() = xmlString.Split(vbCrLf)
        Dim indent As Integer = 0
        Dim length As Integer = 0
        Dim bracketCount As Integer = 0
        For Each line As String In xmlStringArray
            length += line.Length + 1
            If line.IndexOf("<?xml") = -1 AndAlso _
                length < xmlString.Length Then
                If line.IndexOf("<") > -1 Then bracketCount += 1
                If bracketCount > 1 Then
                    If line.IndexOf("</") > -1 Then
                        indent -= 2
                    Else
                        indent += 2
                    End If
                End If
            End If
            builder.AppendLine(Space(indent) & line.Trim())
            If line.Trim().PadRight(2).Substring(0, 2) = "</" Then _
                indent -= 2
        Next
        Return builder.ToString()
    End Function web analytics

Add to Technorati Favorites

Technorati Tags: ,,,,,,,,,,,,,,,

 

Kelly Martens

Occupation
Location
Interests

Poll

Loading...

Favorites

Add to Technorati Favorites

Weather

Loading...
Hey! Let me know you stopped by and if anything interested you! Thanks!
Please wait...
Sorry, the comment you entered is too long. Please shorten it.
You didn't enter anything. Please try again.
Sorry, we can't add your comment right now. Please try again later.
To add a comment, you need permission from your parent. Ask for permission
Your parent has turned off comments.
Sorry, we can't delete your comment right now. Please try again later.
You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
Complete the security check below to finish leaving your comment.
The characters you type in the security check must match the characters in the picture or audio.
marcowrote:
Kelly,

I found this article of yours on the net and I was hoping it could help me building my program.
http://kellychronicles.spaces.live.com/blog/cns!A0D71E1614E8DBF8!1469.entry

I have a vb.net program with a Treeview on the main page (Explorer1).
From a second form (frmZoeken) I want to update the Treeview on the main page but
since I'm a newbie I don't understand the code in the article.

I was hoping that maybe you could help me out.
It would mean a great deal to me.

maybe you will allow me to send you some code I have created?

Marco Krechting
The Netherlands
May 25
Barbarawrote:
Thank you for you very quick response to my note. It works now. Thanks
Mar. 4
Barbarawrote:
I was wondering if you are going to able to update this plugin to work with WLW 2K9? It crashes every time I run it....
Mar. 3
No namewrote:
hey! i love your tag generator addon for WLW, but i think it has problems with WLW 2009 release.. I wrote review on gallery.live.com... Would love if you could updte it :)
Jan. 23
Dev Parikhwrote:
Spaces seems to be up now :)
Dec. 3