.NET Framework Developer Center > .NET Development Forums > .NET Framework Networking and Communication > Problem connecting to YahooStock Quote server using TCPClient
Ask a questionAsk a question
 

AnswerProblem connecting to YahooStock Quote server using TCPClient

  • Saturday, November 07, 2009 1:38 AMKobojunkie Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi,

    I believe this could simply be a syntax error and I would appreciate any help with this. I am able to, using the HTTPWebRequest object to connect to the YahooStock Quote server and retrieve the data I need. However, when I try doing the same using TCPClient and the GET string below

    string request = @"GET /q?s=" + Symbol+ "&d=v1 HTTP/1.0" + "\r\n" + "\r\n";

    I am unable to return the same response which I expect.  I suspect that the string above may be the problem since I am able to connect but just keep getting a Http 404 response. Any idea how I can do this better please?

    Thanks in advance!


    None

Answers

  • Sunday, November 08, 2009 6:46 PMKobojunkie Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    The host was the problem. I guess I needed to set the server as download.finance.yahoo.com



         public YahooStockQuoteClient()
            {
                client = null;
                networkstream = null;
               // ServerAddress = "finance.yahoo.com";
                <strong>ServerAddress = "download.finance.yahoo.com";</strong>
                portnum = 80;
            }
    
            public void ConnectToServer(String quotecode)
            {
                try
                {
                    string request = <strong>"GET /d/quotes.csv?f=sd1t1l1c1&e=.cvs&s=" + quotecode +  "\r\n";</strong>
                    //convert input to bytes
                    inputstream = Encoding.UTF8.GetBytes(request);
                    client = new TcpClient(ServerAddress, portnum);
                    networkstream = client.GetStream();
                    
                    //send request to server via the networkstream object connection 
                    networkstream.BeginWrite(inputstream, 0, inputstream.Length, new AsyncCallback(this.SendCompletedCallback), networkstream);
                    processReturnMessage();
                }
                catch (ArgumentNullException e)
                {
                    Console.WriteLine("ArgumentNullException: {0}", e);
                }
                catch (SocketException e)
                {
                    Console.WriteLine("SocketException: {0}", e);
                }
    
            }
    

    None
    • Marked As Answer byKobojunkie Sunday, November 08, 2009 11:47 PM
    •  

All Replies

  • Sunday, November 08, 2009 4:53 AMFeroze Daud Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Get a tracelog (see instructions in my signature) and compare the request sent between the HttpWebRequest and TcpClient scenarios.

    But I suspect it is because you are not adding the host header.

    I would send the request as

    GET /q?s=<symbol>&d=v1 HTTP/1.0\r\n
    Host: finance.yahoo.com\r\n
    \r\n

    good luck

    feroze
    --
    My blog
    Instruction on how to create a tracelog with your System.Net application
  • Sunday, November 08, 2009 6:46 PMKobojunkie Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    The host was the problem. I guess I needed to set the server as download.finance.yahoo.com



         public YahooStockQuoteClient()
            {
                client = null;
                networkstream = null;
               // ServerAddress = "finance.yahoo.com";
                <strong>ServerAddress = "download.finance.yahoo.com";</strong>
                portnum = 80;
            }
    
            public void ConnectToServer(String quotecode)
            {
                try
                {
                    string request = <strong>"GET /d/quotes.csv?f=sd1t1l1c1&e=.cvs&s=" + quotecode +  "\r\n";</strong>
                    //convert input to bytes
                    inputstream = Encoding.UTF8.GetBytes(request);
                    client = new TcpClient(ServerAddress, portnum);
                    networkstream = client.GetStream();
                    
                    //send request to server via the networkstream object connection 
                    networkstream.BeginWrite(inputstream, 0, inputstream.Length, new AsyncCallback(this.SendCompletedCallback), networkstream);
                    processReturnMessage();
                }
                catch (ArgumentNullException e)
                {
                    Console.WriteLine("ArgumentNullException: {0}", e);
                }
                catch (SocketException e)
                {
                    Console.WriteLine("SocketException: {0}", e);
                }
    
            }
    

    None
    • Marked As Answer byKobojunkie Sunday, November 08, 2009 11:47 PM
    •