Click here to Skip to main content
15,881,757 members
Home / Discussions / C#
   

C#

 
GeneralRe: state management Pin
Urs Enzler13-Sep-07 1:49
Urs Enzler13-Sep-07 1:49 
AnswerRe: state management Pin
Pete O'Hanlon13-Sep-07 1:21
mvePete O'Hanlon13-Sep-07 1:21 
QuestionResolving a mapped drive Pin
benjymous13-Sep-07 0:15
benjymous13-Sep-07 0:15 
AnswerRe: Resolving a mapped drive Pin
Pete O'Hanlon13-Sep-07 1:12
mvePete O'Hanlon13-Sep-07 1:12 
GeneralRe: Resolving a mapped drive Pin
benjymous13-Sep-07 1:37
benjymous13-Sep-07 1:37 
GeneralRe: Resolving a mapped drive Pin
Big Daddy Farang13-Sep-07 11:51
Big Daddy Farang13-Sep-07 11:51 
Questionusing Scrollbar for panning in my Graphics Editor Pin
a_david12313-Sep-07 0:13
a_david12313-Sep-07 0:13 
QuestionWhy The remote server returned an error: (403) Forbidden? [modified] Pin
bug_aonz12-Sep-07 23:57
bug_aonz12-Sep-07 23:57 
I've wrote asynchronous http download files.So i need to test my program by try to download files within my virtual directory but some file can download completely but some can't and thrown WebException "The remote server returned an error: (403) Forbidden".I don't known what's wrong.please help
=(
this is my code

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Net;
using System.IO;
using System.Threading;

namespace DownloadAsync
{
    class Program
    {
        const int BUFFER_SIZE = 1024;

        static void Main(string[] args)
        {
            //ArrayList dataFile = new ArrayList();
            //dataFile.Add("http://localhost/TestFolder/icinst.exe");
            //DownloadItem DI = new DownloadItem(dataFile);
            //DI.StartDownload();
            

            //-------------------------------------------------------------------------
            ArrayList Filedata = new ArrayList();
            StreamReader sr = new StreamReader("filelist.txt");
            string strLine = sr.ReadLine();
            while (strLine != "" && strLine != null)
            {
                string[] data = strLine.Split(',');
                Filedata.Add("http://localhost/TestFolder/" + data[1]);
                Console.WriteLine("Download :- " + data[1]);
                strLine = sr.ReadLine();
            }
            DownloadItem DItem = new DownloadItem(Filedata);
            DItem.StartDownload();

            
            
            Console.WriteLine("Wait...");
            Console.ReadLine();
        }
        
    }
    public class DownloadItem
    {
        private ArrayList FileList = new ArrayList();
        public DownloadItem(ArrayList filelist)
        {
            FileList = filelist;
        }
        public void StartDownload()
        {
            for (int i = 0; i < FileList.Count; i++)
            {
                Thread workThread = new Thread(new ParameterizedThreadStart(DownloadMehtod));
                //workThread.IsBackground=true;
                workThread.Start((string)FileList[i]);
            }
        }
        private void DownloadMehtod(object uri)
        {
            try
            {
                string Uri = uri as string;
                // for each URL in the collection...
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Uri);
                //request.Method = "HEAD";
                string[] ss = Uri.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
                string filename = ss[ss.Length - 1];
                // RequestState is a custom class to pass info
                RequestState state = new RequestState();
                state.request = request;
                state.filename = filename;

                IAsyncResult result = request.BeginGetResponse(
                  new AsyncCallback(RespCallback), state);
                
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
        private void RespCallback(IAsyncResult asynchronousResult)
        {
            try
            {
                
                // Set the State of request to asynchronous.
                RequestState myRequestState = (RequestState)asynchronousResult.AsyncState;
                
                HttpWebRequest myWebRequest1 = myRequestState.request;
                // End the Asynchronous response.
                myRequestState.response = (HttpWebResponse)myWebRequest1.EndGetResponse(asynchronousResult);
                // Read the response into a 'Stream' object.
                Stream responseStream = myRequestState.response.GetResponseStream();
                myRequestState.responseStream = responseStream;
                // Begin the reading of the contents of the HTML page and print it to the console.
                //IAsyncResult asynchronousResultRead = responseStream.BeginRead(myRequestState.bufferRead, 0, BUFFER_SIZE, new AsyncCallback(ReadCallBack), myRequestState);
                //Console.WriteLine(myRequestState.response.Headers);
                //----------------------- Download file
                int ReadCount;
                byte[] buffer = new byte[4096];
                FileStream f = File.Open(myRequestState.filename
                                        , FileMode.Append
                                        , FileAccess.Write, FileShare.None);
                if (myRequestState.responseStream.CanRead)
                {
                    while ((int)(ReadCount = myRequestState.responseStream.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        f.Write(buffer, 0, ReadCount);
                        //Console.WriteLine("ReadCount : " + ReadCount);
                    }
                }
                f.Flush();
                f.Close();
                Console.WriteLine("Completed :-" + myRequestState.filename);
            }
            catch (WebException e)
            {
                Console.WriteLine("WebException raised!");
                Console.WriteLine("\n{0}", e.Message);
                Console.WriteLine("\n{0}", e.Status);
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception raised!");
                Console.WriteLine("Source : " + e.Source);
                Console.WriteLine("Message : " + e.Message);
            }
        }  
    }
    public class RequestState
    {
        // This class stores the state of the request.
        const int BUFFER_SIZE = 1024;
        public string filename;
        public StringBuilder requestData;
        public byte[] bufferRead;
        public HttpWebRequest request;
        public HttpWebResponse response;
        public Stream responseStream;
        public RequestState()
        {
            bufferRead = new byte[BUFFER_SIZE];
            requestData = new StringBuilder("");
            request = null;
            responseStream = null;
        }
    }

}


Could u help me what's wrong?

Thank =)



-- modified at 6:14 Thursday 13th September, 2007
AnswerRe: Why The remote server returned an error: (403) Forbidden? Pin
Le centriste13-Sep-07 0:02
Le centriste13-Sep-07 0:02 
GeneralRe: Why The remote server returned an error: (403) Forbidden? Pin
bug_aonz13-Sep-07 0:23
bug_aonz13-Sep-07 0:23 
GeneralRe: Why The remote server returned an error: (403) Forbidden? Pin
Le centriste13-Sep-07 1:14
Le centriste13-Sep-07 1:14 
GeneralRe: Why The remote server returned an error: (403) Forbidden? Pin
bug_aonz13-Sep-07 16:30
bug_aonz13-Sep-07 16:30 
QuestionSearch for a file Pin
P_Elza12-Sep-07 23:51
P_Elza12-Sep-07 23:51 
AnswerRe: Search for a file Pin
Pete O'Hanlon12-Sep-07 23:59
mvePete O'Hanlon12-Sep-07 23:59 
QuestionInserting a formula in excel Pin
lourensG12-Sep-07 23:51
lourensG12-Sep-07 23:51 
AnswerRe: Inserting a formula in excel Pin
Jimmanuel13-Sep-07 3:02
Jimmanuel13-Sep-07 3:02 
GeneralRe: Inserting a formula in excel Pin
lourensG13-Sep-07 3:10
lourensG13-Sep-07 3:10 
QuestionStrange Issue:Window Service Pin
ramdil12-Sep-07 23:48
ramdil12-Sep-07 23:48 
QuestionRemoting events sometimes failing Pin
Lasse Offt12-Sep-07 23:43
Lasse Offt12-Sep-07 23:43 
AnswerRe: Remoting events sometimes failing Pin
Lasse Offt13-Sep-07 21:19
Lasse Offt13-Sep-07 21:19 
AnswerRe: Remoting events sometimes failing Pin
John Whitmire2-Nov-07 6:17
professionalJohn Whitmire2-Nov-07 6:17 
QuestionString table equivalent in C# Pin
Keshav V. Kamat12-Sep-07 23:42
Keshav V. Kamat12-Sep-07 23:42 
AnswerRe: String table equivalent in C# Pin
Paul Conrad14-Sep-07 19:06
professionalPaul Conrad14-Sep-07 19:06 
QuestionUsing C , C++, VC++ code libraries in C#.net Pin
venkatinaidu12-Sep-07 23:39
venkatinaidu12-Sep-07 23:39 
AnswerRe: Using C , C++, VC++ code libraries in C#.net Pin
Giorgi Dalakishvili12-Sep-07 23:47
mentorGiorgi Dalakishvili12-Sep-07 23:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.