views:

764

answers:

2

Code is below, when I hit the page with:

http://ryan.local.testmode.com/timecrunch/bctimepost.ashx?project=2593059&person=2831215&date=04/16/2009&hours=2.5&case=555

it just sort of hangs with the loading bar moving slowly forever till I stop it, did I forget to do something? I can't figure out which line of code is used to actually 'submit' the request...

NOTE: username/password and [company name] are placeholders for sharing the code and contain the proper values in my running code

using System;
using System.Web;
using System.Xml;
using System.Net;
using System.IO;

public class bctimepost : IHttpHandler {

    public void ProcessRequest(HttpContext context)
    {

        string project_id = context.Request.QueryString["project"];
        string person_id = context.Request.QueryString["person"];
        string post_date = context.Request.QueryString["date"];
        string post_hours = context.Request.QueryString["hours"];
        string case_num = context.Request.QueryString["case"];

        HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create("https://[company name].updatelog.com/projects/" + project_id + "/time_entries.xml");

        objRequest.Method = "POST";
        objRequest.ContentType = "application/xml";
        objRequest.Accept = "application/xml";

        string creds = "username:password";
        byte[] encData_byte = new byte[creds.Length];
        encData_byte = System.Text.Encoding.UTF8.GetBytes(creds);
        string encodedData = Convert.ToBase64String(encData_byte);

        objRequest.Headers.Add("Authorization", "Basic " + encodedData);

        XmlWriterSettings settings = new XmlWriterSettings();
        settings.Indent = true;
        settings.IndentChars = ("    ");

        using (XmlWriter writer = XmlWriter.Create(objRequest.GetRequestStream(), settings))
        {
            writer.WriteStartElement("time-entry");
            writer.WriteElementString("person-id", person_id);
            writer.WriteElementString("date", post_date);
            writer.WriteElementString("hours", post_hours);
            writer.WriteElementString("description", "Worked on Case #" + case_num);
            writer.WriteEndElement();
        }

        HttpWebResponse myHttpWebResponse = (HttpWebResponse)objRequest.GetResponse();

        context.Response.Write(myHttpWebResponse.StatusCode);

    }




    public bool IsReusable {
        get {
            return false;
        }
    }

}
+3  A: 

I think it's that XmlWriter doesn't automatically close the stream returned by GetRequestStream(). Add:

settings.CloseOutput = true;

before creating the XmlWriter and see if that helps.

Jonathan
oh you are SOOO the man
shogun
Yeah, gotta flush the stream after you're done writing to it.
yodaj007
Yes, but flushing the stream won't do much for you if you don't close it.
Jonathan
A: 

As a side note, you should be able to replace this:

string creds = "username:password";
byte[] encData_byte = new byte[creds.Length];
encData_byte = System.Text.Encoding.UTF8.GetBytes(creds);
string encodedData = Convert.ToBase64String(encData_byte);

objRequest.Headers.Add("Authorization", "Basic " + encodedData);

with:

objRequest.Credentials = new NetworkCredential("username", "password");
John Rasch