Thoughts from Dan Miser RSS 2.0
# Thursday, June 01, 2006
In the last in the series of posts on writing a .NET application to communicate over HTTP, I thought I'd talk about HtmlAgilityPack (download available here). This tool allows you to write XPATH-like expressions to parse HTML files easily.

In my application, I upload a file and some form variables to a web server, which then responds with an HTML page. On that HTML page, buried in the middle, is a confirmation ID. In order to easily grab that ID and store it for later use, I use code like the following:


        private string CheckResponse(string response)
        {
            HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
            doc.LoadHtml(response);
            HtmlNodeCollection coll = doc.DocumentNode.SelectNodes("//td[@id]");
            if (coll == null) 
                return null;

            foreach (HtmlNode node in coll)
            {
                if (node.Attributes["id"].Value.Equals("confirmation"))
                {
                    return node.InnerText;
                }
            }

            return null;
        }
Thursday, June 01, 2006 9:18:00 AM (Central Daylight Time, UTC-05:00)  #    Comments [2] -

# Wednesday, May 31, 2006
When using a .NET client to communicate via HTTP, you may notice an HTTP header that looks like this:
Expect: 100-continue

Depending on how stringent the web server is, it may or may not be fine to have this extra header. If you must remove this header, it's very easy to do in .NET 2.0. Just include the following line of code in your application:

ServicePointManager.Expect100Continue = false;

Wednesday, May 31, 2006 9:24:00 AM (Central Daylight Time, UTC-05:00)  #    Comments [1] -

# Tuesday, May 30, 2006
I've been writing a .NET client application that must upload a file to a IBM WebSphere (Java) server. This journey was met with mis-steps all over the place. For starters, it looked like the WebClient class was what I wanted, but it turns out you can't POST both a file and HTML form variables. That meant that I needed to go to the low-level class, HttpWebRequest, to get this done. There is code on google that got me most of the way there, but the web server always ended up rejecting the submission with errors. To make matters worse, I don't own the web server, so support on exactly what was wrong was extremely limited.

Then I stumbled across a paper, Retrieving HTTP content in .NET by Rick Strahl. In that paper, Rick ends up writing a very nice wrapper class to deal with all of the details for you. I put together a test case to try this out, and the amount of code I had to write dropped from 123 lines down to 15. The new code even works with WebSphere. What's more, the wrapper class provides a very nice model for programming applications that need to communicate via HTTP. In my opinion, this is what Microsoft should have delivered with the framework, as opposed to making people write their own wrapper or find Rick's code (which was last updated in 2002, yet still works great).

I made 2 changes to Rick's base class:

  • I changed the cMultiPartBoundary variable to look like this in order to give a unique boundary marker: string cMultiPartBoundary = "-----------------------------" + DateTime.Now.Ticks.ToString("x");
  • In the method GetUrlStream, there is a place where the end boundary marker is written to the stream. However, according to the RFC on uploading files via POST, you need to have 2 trailing dashes to mark the last end. I added those dashes like this: this.oPostData.Write(Encoding.GetEncoding(1252).GetBytes( "--" + this.cMultiPartBoundary + "--\r\n" ) );

I am one happy camper. I've done this very thing with other web applications in Delphi before, and with the Indy components, it was very easy. Now with Rick's wrapper class, it's easy to do the same kind of thing in .NET.

Tuesday, May 30, 2006 12:32:00 PM (Central Daylight Time, UTC-05:00)  #    Comments [4] -
Delphi
# Friday, May 26, 2006
Using LINQ, you can use variables for evaluation of the query. For example, the following query works just fine. It would also work if you used something like textBox1.Text instead of the variable s.


    string s = "Beverages";
    var results = from c in db.Categories
                  where c.CategoryName == s
                  select c.CategoryName;
Friday, May 26, 2006 8:18:00 AM (Central Daylight Time, UTC-05:00)  #    Comments [0] -
LINQ
# Wednesday, May 24, 2006
I recently needed to monitor the HTTP traffic of an application that I wrote in order to debug why things were failing. Unfortunately, a simple proxy wouldn't work since the HTTPS traffic is encryped by SSL. This makes sense, since you really would hope that the data you send when surfing on a secure web page would be secure. I ended up solving this problem the same way you can solve every programming problem in the world: add a layer of indirection.

To start, I downloaded and installed the latest binaries of stunnel. Before starting, I modified the conf file to add the following entry:


[dor]
accept=localhost:8080
connect=www.MySuperSecureSite.com:443

Then, I installed tcpTrace and set things up to Listen on port 8079 and forward to localhost:8080. Lastly, in my application, I connected to http://localhost:8079/MyPath. This would eventually forward all of the traffic to the real destination, but I was able to spy on the details in tcpTrace since that was just HTTP traffic.

After doing this, I could see where things were malformed and clean my code up to submit things in the desired format.

Wednesday, May 24, 2006 2:08:00 PM (Central Daylight Time, UTC-05:00)  #    Comments [0] -

# Tuesday, May 16, 2006
In the README for the LINQ download, one big feature that was mentioned was the ability to use an external XML mapping file instead of attributes. This decouples the schema information from the code, which is good as a database schema tends to change over time, and you don't want to recompile every time your DBA decides to change something on you. The down side is that there isn't much information out there on how to get it to work. Hopefully, this entry fills that gap.

The first thing to note is the syntax of the XML mapping file. You can take a look at LINQ Preview\Docs\DLINQ Mapping Schema.xsd for more information on the mapping format, but here is a shortened extract from LINQ Preview\Data\samplemapping.xml to give you a look at a concrete implementation:


<?xml version="1.0" encoding="Windows-1252"?>
<Database Name="Northwind">
  <Table Name="Categories">
    <Type Name="Mapping.Category">
      <Column Name="CategoryID" Member="CategoryID" Storage="_CategoryID" DbType="Int NOT NULL IDENTITY" IsIdentity="True" IsAutoGen="True" />
      <Column Name="CategoryName" Member="CategoryName" Storage="_CategoryName" DbType="NVarChar(15) NOT NULL" />
      <Column Name="Description" Member="Description" Storage="_Description" DbType="NText" UpdateCheck="Never" />
      <Column Name="Picture" Member="Picture" Storage="_Picture" DbType="Image" UpdateCheck="Never" />
      <Association Name="FK_Products_Categories" Member="Products" Storage="_Products" ThisKey="CategoryID" OtherTable="Products" OtherKey="CategoryID" />
    </Type>
  </Table>
</Database>

The above code shows how the Categories table in the Northwind database could be mapped in an XML file. One thing of special note here is the Association element which will allow data hierarchies to be established, much like you can do in the DLINQ designer. You can generate this file by hand if you want, or you can use the new version of LINQ Preview\bin\sqlMetal.exe to generate the starting point for you. sqlMetal has a lot of options for you to look at, including a bunch to take save snapshots of database schema information to an xml file, and generate classes and XML mapping files from that saved XML file. A sample command would look like this:


sqlmetal /server:(local) /database:Northwind /map:nwindmapping.xml /namespace:Mapping /code:nwind.cs

The resulting nwind.cs file does not contain any attributes, as they are all stored in the xml file. To use this file, use code like this:


    XmlMappingSource mappingSource = XmlMappingSource.FromXml(File.ReadAllText("nwindmapping.xml"));
    Mapping.Northwind db = new Mapping.Northwind("Integrated Security=SSPI;database=northwind;server=(local)", mappingSource);
    var q = from c in db.Categories 
            select c.CategoryName;
    foreach (string s in q)
        listBox1.Items.Add(s);
Tuesday, May 16, 2006 1:10:00 PM (Central Daylight Time, UTC-05:00)  #    Comments [2] -
LINQ
# Friday, May 12, 2006
While ActiveX has enjoyed a (mostly) deserved reputation for being ill-tempered and hard to work with, I've used it successfully for many years as a deployment vehicle for rich-client web applications that run inside the context of IE. I'll attribute most of that success to Delphi and ActiveForms since the most complicated plumbing is taken care of for me automatically, but still gives me the ability to override what I need. Special thanks should go to Lino Tadros and Steve Teixeira, former members of the Delphi R&D team, to allow this.

Fast forward to today. My goal is relatively simple. I have a .NET application built with WinForms. I'd like the same type of ease of use in deploying this WinForm application to my users. I've done quite a bit of research on the best way to achieve this, but I haven't come up with the perfect solution yet. I'm close, but it's not buttoned up all of the way yet. For starters, I would highly recommend the following articles:

I wasn't able to find this nugget anywhere, though. If you want to call methods from your HTML page via (e.g.) JScript, you need to make your EXE assembly COM visible. The easiest way to do this is to select Properties for the EXE in the Project Manager, and press Assembly Information. There, you'll find a checkbox to make this assembly COM visible.

So far, I've been able to get a WinForm EXE hosted in IE, strong-name it and communicate from the web page to the control. I still have several items left to tackle, like wrapping everything up in a CAB file for easier deployment, and getting calls to other assemblies working. I'll comment on those as I get to them, but if you have any pointers on better ways to do any of this, I'm all ears!

Friday, May 12, 2006 3:29:00 PM (Central Daylight Time, UTC-05:00)  #    Comments [3] -
Delphi
# Wednesday, May 10, 2006
LINQ is coming along nicely. This release is full of goodies. Among my favorites: join support, DLINQ graphical designer, LINQ over DataSet, multi-tier enhancements, and improved conflict resolution. But the thing I'm most excited about is the addition of XML mapping of elements! I attended a talk on ORM by Sean McCormack at the WI .NET User Group last night. He is an excellent speaker and spoke about DLINQ for a couple minutes, but hated the fact that it didn't have XML mapping. Maybe this will bring him around. :-) It also looks like I'll be speaking at the WI .NET User Group on LINQ in the medium-term future. I'll be sure to post more about that event and more findings with LINQ. In the meantime, download the CTP now.
Wednesday, May 10, 2006 7:51:00 PM (Central Daylight Time, UTC-05:00)  #    Comments [0] -
LINQ
Navigation
Archive
<June 2006>
SunMonTueWedThuFriSat
28293031123
45678910
11121314151617
18192021222324
2526272829301
2345678
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2013
Dan Miser
Sign In
Statistics
Total Posts: 391
This Year: 2
This Month: 2
This Week: 0
Comments: 674
Themes
Pick a theme:
All Content © 2013, Dan Miser
DasBlog theme 'Business' created by Christoph De Baene (delarou)