User Agent definition when consuming web services using HTTP in BizTalk 2010

January 10, 2012 at 3:49 pm Leave a comment

Last month I started to develop a BizTalk 2010 application capable of interacting with some ASMX web services exposed by an in-house developed application framework built in .Net Framework 2.0

This custom made framework dynamically defines the schema of the responses delivered by the web services. There is no fixed structure for the BizTalk Web Reference Wizard to interpret and it utterly fails with the following message:

image

To overcome this I defined a method in a helper class that could be capable of consuming the web service using a simple HTTP POST operation:

using System.Net;
using System.IO;

WebRequest webRequest = WebRequest.Create(uri);
webRequest.Method = "POST";
if (timeout == "") webRequest.Timeout = 300000;
else webRequest.Timeout = Int32.Parse(timeout);
webRequest.Headers.Add("SOAPAction", soapAction);
byte[] bytes = Encoding.ASCII.GetBytes(messageRequest.InnerXml);
Stream os = null;
StreamReader sr = null;

try
{
    webRequest.ContentLength = bytes.Length;
    os = webRequest.GetRequestStream();
    os.Write(bytes, 0, bytes.Length);
}
catch (WebException ex)
{
    error = true;
    this.errorDescription = ex.Message;
}
finally
{
    if (os != null)
    {
        os.Close();
    }
}

if (error == false)
try
{
    WebResponse webResponse = webRequest.GetResponse();
    if (webResponse != null)
    {
        sr = new StreamReader(webResponse.GetResponseStream());
        this.xmlOut.LoadXml(sr.ReadToEnd().Trim());
    }
    webResponse.Close();
}
catch (WebException ex)
{
    this.errorDescription = ex.Message;
}
finally
{
    if (sr != null)
    {
        sr.Close();
    }
}

I unit tested my code against some of the dynamically generated web servers and everything worked just fine, so I proceeded to move the application to a BizTalk QAS environment in order to perform more comprehensive integral tests.

For two weeks everything worked ok and I started to prepare for the eventual deploy of the application to the production servers. However suddenly last Wednesday the execution of the web services started to fail with the following error:

image

I debugged my code and found out the xml Request message was correctly posted to the ASMX and a 202 code was returned. The real problem happened when the Response was dynamically generated by the web service and the aforementioned error was generated.

I contacted the developer of the web services and along with him debugged his code and found out a new addition to the original method in charge of generating the Response schema. A new condition had been added to the code in order to evaluate the User Agent received by the web service and define the proper schema for each case:

string userAgent = System.Web.HttpContext.Current.Request.UserAgent.ToLower().Substring(0,5);
switch (userAgent)
{
    case "java/":
    case "axis/":
       xmlSchema = new XmlSchemaProviderForCrystalReport(nameProvider, baseLocation);
       break;
    default:
       xmlSchema = new XmlSchemaProviderForDotNet(nameProvider, baseLocation);
    break;
 }
return xmlSchema;

With this information in hand I started a network capture with Microsoft Network Monitor 4.0 and ran two more test, one with my BizTalk application and another with soapUI 4.0.1. When the capture ended I compared the information sent by both methods to the web service and found out my code wasn’t generating a User Agent value at all. Some how I have assumed the WebRequest would automatically add the proper value.

My Code image
SOAPUI image

 

Now I knew my helper class was working just fine, I only needed to send the proper User Agent value to the web services. Problem was the WebRequest Class doesn’t have User Agent as a valid property. This is because it is an abstract class capable of use any protocol. If you want to access specific HTTP properties like User Agent you need to use HttpWebRequest:

((HttpWebRequest)webRequest).UserAgent = @"Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)";

User Agent is a simple string so you can define any value you want. In my case I configured the one corresponding to Internet Explorer 10.

With this change in place the communication was reestablished with the web service and the right response correctly generated each and every time.

Advertisements

Entry filed under: BizTalk. Tags: , , , , , .

How to set up Correlation Sets in BizTalk 2010 Configuring the WSS adapter in BizTalk 2010

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


January 2012
M T W T F S S
« Nov   Feb »
 1
2345678
9101112131415
16171819202122
23242526272829
3031  

Categories


%d bloggers like this: