socket gives different result than webClient

M

Matthijs de Z

Hi,

Since I have a problem related to NOD32, using WebClient / WebRequest,
I wanted to make a socket connection to get the data I need.

But for some reason, the page string SocketSendReceive in method is
not the same as string EODInfo in the main method.

I used practically the same code for the socket method as provided in
the MSDN page: http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.aspx

Could someone help me out how to get the same results in string data
as in EODInfo.
Kind regards,

Matthijs

Console App:

using System;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Diagnostics;

public class GetSocket
{
private static Socket ConnectSocket(string server, int port)
{
Socket s = null;
IPHostEntry hostEntry = null;

hostEntry = Dns.GetHostEntry(server);

foreach (IPAddress address in hostEntry.AddressList)
{
IPEndPoint ipe = new IPEndPoint(address, port);
Socket tempSocket =
new Socket(ipe.AddressFamily, SocketType.Stream,
ProtocolType.Tcp);

tempSocket.Connect(ipe);

if (tempSocket.Connected)
{
s = tempSocket;
break;
}
else
{
continue;
}
}
return s;
}

private static string SocketSendReceive(string server, int port)
{
string request = "GET \r\nHost: " + server +
"\r\nConnection: Close\r\n\r\n";

Byte[] bytesSent = Encoding.ASCII.GetBytes(request);
Byte[] bytesReceived = new Byte[256];

Uri uri = new Uri(@"http://" + server);
string host = uri.Host;

Socket s = ConnectSocket(host, port);

if (s == null)
return ("Connection failed");

s.Send(bytesSent, bytesSent.Length, SocketFlags.None);

int bytes = 0;
string page = String.Empty;

do
{
bytes = s.Receive(bytesReceived, bytesReceived.Length, 0);
page = page + Encoding.ASCII.GetString(bytesReceived, 0,
bytes);
}
while (bytes > 0);

Debug.WriteLine(page);
return page;
}

public static void Main(string[] args)
{
string webaddress = "ichart.finance.yahoo.com/table.csv?
s=AF.PA&a=0&b=1&c=2009&d=9&e=19&f=2010&g=d&ignore=.csv";
int port = 80;

string result = String.Empty;
Debug.WriteLine("Start Socket method");
result = SocketSendReceive(webaddress, port);
Debug.WriteLine(result);


Debug.WriteLine("Start webClient method");
using (WebClient wc = new WebClient())
{
Debug.WriteLine("Trace:");
System.Diagnostics.Trace.WriteLine(@"http://" +
webaddress);
Debug.WriteLine("EOD content");
string EODInfo = wc.DownloadString(@"http://" +
webaddress);
Debug.WriteLine("EOD content");
Debug.WriteLine(EODInfo);
}

Console.ReadLine();
}
}
 
M

Matthijs de Z

Solved:
string request = "GET /table.csv?
s=AF.PA&a=0&b=1&c=2009&d=9&e=19&f=2010&g=d&ignore=.csv \r\nHost: " +
server +
"\r\nConnection: Close\r\n\r\n";
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top