Thursday, April 24, 2014

The remote server returned an error: (400) Bad Request.

In one of the projects that I was working on I started getting this error as soon as the site was hosted on the test server.
To give a brief background on the requirement, I had a site that was making a HttpWebRequest to the home page of another site to get the cookies.These cookies were then reused in subsequent requests.The sample code which was working on my dev machine is below

HttpWebRequest oRequest = (HttpWebRequest)WebRequest.Create(new Uri(sSiteHomeUrl));
oRequest.Method = "GET";
oRequest.ContentType = @"application/x-www-form-urlencoded";
oRequest.CookieContainer = oCookies;
oRequest.Accept = @"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
oRequest.UserAgent = @"CHROME browser version: 33.0 user agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36";
oRequest.Headers["Accept-Encoding"] = @"gzip,deflate,sdch";
oRequest.Headers["Accept-Language"] = @"en-US,en;q=0.8,de;q=0.6,es;q=0.4";

string sResponseData = string.Empty;          
using(WebResponse oWebResponse = oRequest.GetResponse())
using (Stream data = oWebResponse.GetResponseStream())
using (StreamReader reader = new StreamReader(data))
{
      sResponseData = reader.ReadToEnd();
}

Once I tested it on my dev machine I moved the code and hosted the application on the test server.However on the test server I started getting this error.After a day of struggling and going through various forums I was able to fix the issue.

The problem was that both my site that was making a call to another site were on the same server under the same IIS. When creating the URL I was using the server name in the url.By replacing the servername with the Ip address I was able to successfully make the request from my site and get the cookies for subsequent requests.

Old call
http://servername:12348/Home.aspx

New call
http://ipaddressofserver:12348/Home.aspx

Friday, April 18, 2014

c# - Get all attribute values that exceeds int32 max value in an xml

I maintain an application that collects millions of xml files.I have parsers that then run to extract the information from these xml files.Recently we noticed that there was a drop in the number of xml files we were receiving.
We started seeing this error in the logs that caused the drop in the xml posts

System.OverflowException: Value was either too large or too small for an Int32.

So we grabbed one of the xml files that was failing and looked for any attribute that was causing the xml post to fail.The xml file size varies on the data and it was hard to figure out which attribute was causing the failure.
In order to figure this out I wrote a small program to list all the attributes that had a value which exceeded the Int32 value.Using this we were able to identify the data limits and fix it. This code can be tweaked to check for boolean,long,date,string size etc. Below is the code

using System;
using System.Xml.Linq;

namespace GetInvalidXmlIntElement

{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                var oXmlDoc = XDocument.Load(@"c:\Data\Sample.xml");
                var oElements = oXmlDoc.Descendants();
                var iElementsCounter = 0;
                var iAttributeCounter = 0;
                var iInvalidValueCounter = 0;
                foreach (var oElement in oElements.Elements())
                {
                    iElementsCounter++;
                    foreach (var oAttribute in oElement.Attributes())
                    {
                        iAttributeCounter++;
                        try
                        {
                            if (Convert.ToInt64(oAttribute.Value) > Int32.MaxValue)
                                Console.WriteLine(" {0}) Element {1} Attribute {2} Int32 Max {3} Value {4} ",++iInvalidValueCounter,oElement.Name, oAttribute.Name, Int32.MaxValue, Convert.ToInt64(oAttribute.Value));
                        }
                        catch { }
                    }
                }
                Console.WriteLine();
                Console.WriteLine("Total Number of Elements in the Xml is:{0}", iElementsCounter);
                Console.WriteLine("Total Number of Attributes in the Xml is:{0}", iAttributeCounter);

            }

            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.Read();
        }
    }
}




Friday, April 11, 2014

Task Scheduler 0x1

In one of the projects I am working on,I had to create a few scheduled tasks in the Task Scheduler.The server is running on Windows Server 2008 OS.I created the tasks but when it ran I saw the following 0x1 status in the last run result column of the task scheduler.

Neither the event viewer showed any error nor the history tab for the tasks.The solution was that I had not set the working directory for the task.After setting the working directory the tasks ran without any issues.

See below screenshots



















Hope that helps

c# - Delete browser cookies,cache

 Directory.Delete(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache),true);

Tuesday, April 1, 2014

UPDATE failed because the following SET options have incorrect settings: 'ARITHABORT'

While testing one of the web applications I built, I started getting this error on one of the stored procedures.The application was using 3 databases to manage data and the users would make changes to a local database and after reviewing the changes would move the changes to the staging and then an admin would move all the changes by the users to the final database.So,In my original test application setup,I had 3 databases i.e. local,staging,final on the same server and the stored procedure to update the staging from local database was working.

However when we moved the test database to a different server and modified the stored procedure to point to the new test server we received this error.

UPDATE failed because the following SET options have incorrect settings: 'ARITHABORT'. Verify that SET options are correct for use with indexed views and/or indexes on computed columns and/or filtered indexes and/or query notifications and/or XML data type methods and/or spatial index operations.

The fix was to reconfigure the user options on the database that was moved to the new server

sp_configure 'user options', 64
reconfigure