Jump to content

[SOLVED] Odd thing happening when I download file with httpwebrequest


Recommended Posts

I'm downloading an XML file from a remote server using httpwebrequest.  Here is my code:

 

using System;
using System.IO;
using System.Net;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void getXML_Click(object sender, EventArgs e)
        {
            byte[] buf = new byte[8192];
            if (gamertagBox.Text != "")
            {
                string gamertag = gamertagBox.Text;
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://www.bungie.net/stats/halo3/PlayerScreenshotsRss.ashx?gamertag=" + gamertag);
                HttpWebResponse response = (HttpWebResponse)req.GetResponse();
                Stream responseStream = response.GetResponseStream();
                if (response.Headers["Content-Type"] == "text/html")
                {
                    MessageBox.Show("This gamertag is not valid.");
                }
                else
                {
                    string respString = "";
                    int count = 0;
                    do
                    {
                        count = responseStream.Read(buf, 0, buf.Length);
                        if (count != 0)
                        {
                            respString += Encoding.ASCII.GetString(buf, 0, count);
                        }
                    }
                    while (count > 0);
                    XMLResult.Text = respString;
                }
            }
            else
            {
                MessageBox.Show("You must enter a gamertag");
            }
        }
    }
}

 

This code retrieves an xml file specific to the gamertag entered, then prints the result to a multiline textbox.  Well here is the first little bit of the returned data:

 

???<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
	<atom:icon>http://www.bungie.net/images/Bungie-Logo-PicLens-white.png</atom:icon>
	<title>Recent Halo 3 Screenshots from HaLo2FrEeEk</title>
	<link>http://www.bungie.net/Stats/Halo3/Screenshots.aspx?player=HaLo2FrEeEk</link>
	<description>Most recent Halo 3 screenshots taken by a particular gamertag.</description>
	<language>en-us</language>
	<pubDate>Thu, 19 Nov 2009 02:46:32 GMT</pubDate>
	<docs>http://blogs.law.harvard.edu/tech/rss</docs>
	<generator>BlamDotNet RSS Generator</generator>
	<webMaster>[email protected] (Bungie WebMaster)</webMaster>

 

See the ??? at the beginning?  I don't know how that's getting there.  It's always there, even if I don't do the do while loop.  It's not a huge issue, but I'd really like that to not be there, anyone have any ideas why it might be there and what I can do so that it doesn't get put there.  And also if there's a better way to do what I'm doing, I just want to parse the xml for it's values.

Nevermind, sorry.  I figured it out.  Turns out having it as a bytereader wasn't the best method, I could simply create a streamreader(response.getresponsestream()) then readtoend() that streamreader.  It works fine.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.