StephenLiversidge
asked on
get the http response stream in c#
Hi all,
heres what ive been asked to do, I need to read the http responses of a website into a database, im working in c# and not to sure how to go about this one.
heres what ive been asked to do, I need to read the http responses of a website into a database, im working in c# and not to sure how to go about this one.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
str path is the url and assumed t5hat the respnse contains an xml u can cahnge it to ur requirement...
here is a class I use in my project(s):
#region HTTP Request
public class HTTPRequest
{
private string serverIp = "";
private int serverPort = 0;
private string serverUsername = "";
private string serverPassword = "";
public HTTPRequest(string ip,int port,string username,string password) {
serverIp = ip;
serverPort = port;
serverUsername = username;
serverPassword = password;
}
public string Submit(string servlet, string param)
{
return Submit("/"+servlet+"?"+par am);
}
public string Submit(string url)
{
int tries = 0;
string s = "";
TcpClient tcp = new TcpClient();
tcp.Connect(serverIp,serve rPort);
tcp.ReceiveTimeout = 20000;
s = SubmitTcp(tcp,Header(url)) ;
tcp.Close();
return s;
}
private string Header(string url) {
string header = "";
UTF8Encoding Uuserpass = new UTF8Encoding();
string userpass = Convert.ToBase64String(Uus erpass.Get Bytes(serv erUsername +":"+serve rPassword) );
header = "GET "+url+" HTTP/1.1\r\nAuthorization: Basic "+userpass+"\r\n\r\n";
header += "Content-type: application/x-www-form-url encoded\r\ n";
header += "Content Length: "+url.Length+"\r\n";
header += "Connection: close\r\n\r\n";
return header;
}
private string SubmitTcp(TcpClient tcp,string header) {
string response = "";
ASCIIEncoding encode = new ASCIIEncoding();
byte[] b = encode.GetBytes(header);
Stream stream = tcp.GetStream();
stream.Write(b,0,b.Length) ;
byte[] ret = new byte[1000];
int k = 0;
while ((k=stream.Read(ret,0,1000 )) > 0) {
for (int i=0;i<k;i++) response += (Convert.ToChar(ret[i]));
}
stream.Close();
return response;
}
}
#endregion
#region HTTP Request
public class HTTPRequest
{
private string serverIp = "";
private int serverPort = 0;
private string serverUsername = "";
private string serverPassword = "";
public HTTPRequest(string ip,int port,string username,string password) {
serverIp = ip;
serverPort = port;
serverUsername = username;
serverPassword = password;
}
public string Submit(string servlet, string param)
{
return Submit("/"+servlet+"?"+par
}
public string Submit(string url)
{
int tries = 0;
string s = "";
TcpClient tcp = new TcpClient();
tcp.Connect(serverIp,serve
tcp.ReceiveTimeout = 20000;
s = SubmitTcp(tcp,Header(url))
tcp.Close();
return s;
}
private string Header(string url) {
string header = "";
UTF8Encoding Uuserpass = new UTF8Encoding();
string userpass = Convert.ToBase64String(Uus
header = "GET "+url+" HTTP/1.1\r\nAuthorization:
header += "Content-type: application/x-www-form-url
header += "Content Length: "+url.Length+"\r\n";
header += "Connection: close\r\n\r\n";
return header;
}
private string SubmitTcp(TcpClient tcp,string header) {
string response = "";
ASCIIEncoding encode = new ASCIIEncoding();
byte[] b = encode.GetBytes(header);
Stream stream = tcp.GetStream();
stream.Write(b,0,b.Length)
byte[] ret = new byte[1000];
int k = 0;
while ((k=stream.Read(ret,0,1000
for (int i=0;i<k;i++) response += (Convert.ToChar(ret[i]));
}
stream.Close();
return response;
}
}
#endregion
ASKER
Hi, Im getting this message at the min, very frustrating
The server committed a protocol violation. Section=ResponseHeader Detail=CR must be followed by LF
The url im trying to grab is www2.url.com though, would this make a difference?
ASKER
Thanks, everyone
All sorted now
All sorted now