Http handler - need some help

T

Taryon

Hi all!
I am using the code from the article:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetcomp/html/httpcomm.asp

well. i made some changes. first changes in the server program:



using System;
using System.Web;
using System.IO;
using System.Data.Odbc;
using System.Text;
using System.Diagnostics;


namespace HandlerLib
{
public class DeliveryDataHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
Trace.Listeners.Add(new TextWriterTraceListener("c:\\trace.txt"));
Trace.AutoFlush=true;
OdbcConnection conn, cnn;
// Attach to the request stream
Stream requestStream = context.Request.InputStream;

StreamReader rdr = new StreamReader(requestStream);
Stream responseStream = context.Response.OutputStream;
StreamWriter wrtr = new StreamWriter(responseStream);

// Read the incoming data, counting the lines
conn=new OdbcConnection("Driver={SQL Server}; Server=192.168.0.3;
Address=192.168.0.3,1433; Network=DBMSSOCN; Database=DELIVERY; Uid=sa;
Pwd=saka");
conn.Open();
cnn=new OdbcConnection("Driver={SQL Server}; Server=192.168.0.3;
Address=192.168.0.3,1433; Network=DBMSSOCN; Database=DELIVERY; Uid=sa;
Pwd=saka");
cnn.Open();
string inLine = rdr.ReadLine();
string[] codes = inLine.Split('=');
Trace.WriteLine(inLine);
switch (codes[0])
{
case "FLAGS":
string xFlags = codes[1];
string telephone = rdr.ReadLine();

// Attach to the response stream and wrap in a StreamWriter

string sql="SELECT * FROM PACKETS WHERE FLAGS='"+xFlags+"'";
OdbcCommand cmd=new OdbcCommand(sql,conn);
OdbcDataReader d1= cmd.ExecuteReader();
if (d1.Read())
{
string sql2="SELECT COUNT(ORDER) AS EXP1 FROM ORDERS WHERE
FLAGS='"+xFlags+"'";
OdbcCommand cmd2 = new OdbcCommand(sql2,cnn);
OdbcDataReader d2 = cmd2.ExecuteReader();
bool lone = d2.Read();
wrtr.WriteLine("FLAGS="+d1["FLAGS"].ToString());
wrtr.WriteLine("STATE_FLAGS="+d1["STATE_FLAGS"].ToString());
wrtr.WriteLine("CITY_FLAGS="+d1["CITY_FLAGS"].ToString());
wrtr.WriteLine("PUT="+d1["PGU"].ToString());
wrtr.WriteLine("RTJ="+d1["RTB"].ToString());
wrtr.WriteLine("CNPJ="+d1["CNPJ"].ToString());
wrtr.WriteLine("CPF="+d1["CPF"].ToString());
wrtr.WriteLine("MARCA="+d1["MARCA"].ToString());
wrtr.WriteLine("MODEL="+d1["MODEL"].ToString());
wrtr.WriteLine("SPECIE="+d1["SPECIE"].ToString());
wrtr.WriteLine("ANO="+d1["ANO"].ToString());
wrtr.WriteLine("TIPO="+d1["TIPO"].ToString());
wrtr.WriteLine("USER="+d1["USER"].ToString());
wrtr.WriteLine("ORDERS="+d2["EXP1"].ToString());
wrtr.Flush();
d1.Close();
}
else
{
wrtr.WriteLine("*****");
wrtr.Flush();
}
break;

case "ORDER":
try
{
// Read the incoming data
inLine=codes[1];

string sqla="INSERT INTO DELIVERY (ORDER, MOTIVACAO,DATAOCORRENCIA,
HORAOCORRENCIA,";
sqla+="COUNTY, TIPO_LOGRADOURO, LOGRADOURO, ";
sqla+="FLAGS, STATE_FLAGS, CITY_FLAGS, TIPO_ORDER, NOME_DELIVERY, ";
sqla+="PUT, RTJ, CNPJ, CPF, MARCA, MODELO, SPECIE, ANO, TIPO) VALUES
(";

string pvez="'";
while (inLine != null)
{
sqla+=pvez+inLine+"'";
inLine = rdr.ReadLine();

pvez=",'";
if (inLine=="*") break;
}
sqla+=")";
Trace.WriteLine(sqla);
string s1 = "insert into table1 (message) values ('"+sqla+"')";
OdbcCommand c1 = new OdbcCommand(s1,conn);
c1.ExecuteNonQuery();

rdr.Close();
wrtr.WriteLine("END");
wrtr.Flush();
//sw.Close();
}
catch (Exception ex)
{
string s = "insert into table1 (message) values ('"+ex.Message+"')";
OdbcCommand c=new OdbcCommand(s,conn);
c.ExecuteNonQuery();

}
break;
}
}

public bool IsReusable
{
get {return false;}
}

}

}




and then in the client form1 code. (need some changes in the design too.
that is, take off some components.


using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.IO;



namespace DeliverySmartClient
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox serverUrlTextbox;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.MainMenu mainMenu1;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.serverUrlTextbox = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
//
// serverUrlTextbox
//
this.serverUrlTextbox.Location = new System.Drawing.Point(16, 40);
this.serverUrlTextbox.Size = new System.Drawing.Size(208, 20);
this.serverUrlTextbox.Text =
"http://192.168.0.2/HandlerVdir/foo.delivery";
//
// label2
//
this.label2.Location = new System.Drawing.Point(24, 16);
this.label2.Text = "Server Message";
//
// button1
//
this.button1.Location = new System.Drawing.Point(80, 80);
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.Controls.Add(this.button1);
this.Controls.Add(this.label2);
this.Controls.Add(this.serverUrlTextbox);
this.Menu = this.mainMenu1;
this.MinimizeBox = false;
this.Text = "ASP.NET Handler Client";

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>

static void Main()
{
Application.Run(new Form1());
}



private void button1_Click(object sender, System.EventArgs e)
{

HttpWebRequest req1 =
(HttpWebRequest)WebRequest.Create(serverUrlTextbox.Text);
req1.Method = "PUT";
req1.AllowWriteStreamBuffering = true;
req1.KeepAlive=true;

// Retrieve request stream and wrap in StreamWriter
Stream reqS = req1.GetRequestStream();
StreamWriter wrtr1 = new StreamWriter(reqS);

wrtr1.WriteLine("ORDER=5555");
wrtr1.WriteLine("afd");
wrtr1.WriteLine(DateTime.Now.ToShortDateString());
wrtr1.WriteLine(DateTime.Now.ToShortTimeString());
wrtr1.WriteLine("afd");
wrtr1.WriteLine("afd");
wrtr1.WriteLine("LASTGOOD");
wrtr1.WriteLine("FIRSTERROR");
wrtr1.WriteLine("afdasdf");
wrtr1.WriteLine("afdasdf");
wrtr1.WriteLine("afdasdf");
wrtr1.WriteLine("afdasdf");
wrtr1.WriteLine("afdasdf");
wrtr1.WriteLine("afdasdf");
wrtr1.WriteLine("afdasdf");
wrtr1.WriteLine("afdasdf");
wrtr1.WriteLine("afdasdf");
wrtr1.WriteLine("afdasdf");
wrtr1.WriteLine("afdasdf");
wrtr1.WriteLine("afdasdf");
wrtr1.WriteLine("afdasdf");

wrtr1.WriteLine("*");

wrtr1.Flush();
wrtr1.Close();

// Submit the request and get the response object
HttpWebResponse resp1 = (HttpWebResponse) req1.GetResponse();

// Retrieve the response stream and wrap in a StreamReader
Stream respStream1 = resp1.GetResponseStream();
StreamReader rdr1 = new StreamReader(respStream1);
// Read through the response line-by-line
string inLine1 = rdr1.ReadLine();
rdr1.Close();


MessageBox.Show("Transmited with success!");
}
}
}



the problem is. when i execute the system as is, i got an error INTERNAL
SERVER ERROR. but if i take off part of the sqla string to have this:



string sqla="INSERT INTO DELIVERY (ORDER, MOTIVACAO,DATAOCORRENCIA,
HORAOCORRENCIA, COUNTY, TIPO_LOGRADOURO, LOGRADOURO) VALUES (";


and change the client to send only the lines to this sql, the code works.
any help will be welcome!

thx in advance

PS. you need to make the original project works first. then change it.
 
T

Taryon

rebuild the system from the scratch and works.
dont ask me what was the problem. i dont know. hahahah
sds

Taryon said:
Hi all!
I am using the code from the article:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetcomp/html/httpcomm.asp

well. i made some changes. first changes in the server program:



using System;
using System.Web;
using System.IO;
using System.Data.Odbc;
using System.Text;
using System.Diagnostics;


namespace HandlerLib
{
public class DeliveryDataHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
Trace.Listeners.Add(new TextWriterTraceListener("c:\\trace.txt"));
Trace.AutoFlush=true;
OdbcConnection conn, cnn;
// Attach to the request stream
Stream requestStream = context.Request.InputStream;

StreamReader rdr = new StreamReader(requestStream);
Stream responseStream = context.Response.OutputStream;
StreamWriter wrtr = new StreamWriter(responseStream);

// Read the incoming data, counting the lines
conn=new OdbcConnection("Driver={SQL Server}; Server=192.168.0.3;
Address=192.168.0.3,1433; Network=DBMSSOCN; Database=DELIVERY; Uid=sa;
Pwd=saka");
conn.Open();
cnn=new OdbcConnection("Driver={SQL Server}; Server=192.168.0.3;
Address=192.168.0.3,1433; Network=DBMSSOCN; Database=DELIVERY; Uid=sa;
Pwd=saka");
cnn.Open();
string inLine = rdr.ReadLine();
string[] codes = inLine.Split('=');
Trace.WriteLine(inLine);
switch (codes[0])
{
case "FLAGS":
string xFlags = codes[1];
string telephone = rdr.ReadLine();

// Attach to the response stream and wrap in a StreamWriter

string sql="SELECT * FROM PACKETS WHERE FLAGS='"+xFlags+"'";
OdbcCommand cmd=new OdbcCommand(sql,conn);
OdbcDataReader d1= cmd.ExecuteReader();
if (d1.Read())
{
string sql2="SELECT COUNT(ORDER) AS EXP1 FROM ORDERS WHERE
FLAGS='"+xFlags+"'";
OdbcCommand cmd2 = new OdbcCommand(sql2,cnn);
OdbcDataReader d2 = cmd2.ExecuteReader();
bool lone = d2.Read();
wrtr.WriteLine("FLAGS="+d1["FLAGS"].ToString());
wrtr.WriteLine("STATE_FLAGS="+d1["STATE_FLAGS"].ToString());
wrtr.WriteLine("CITY_FLAGS="+d1["CITY_FLAGS"].ToString());
wrtr.WriteLine("PUT="+d1["PGU"].ToString());
wrtr.WriteLine("RTJ="+d1["RTB"].ToString());
wrtr.WriteLine("CNPJ="+d1["CNPJ"].ToString());
wrtr.WriteLine("CPF="+d1["CPF"].ToString());
wrtr.WriteLine("MARCA="+d1["MARCA"].ToString());
wrtr.WriteLine("MODEL="+d1["MODEL"].ToString());
wrtr.WriteLine("SPECIE="+d1["SPECIE"].ToString());
wrtr.WriteLine("ANO="+d1["ANO"].ToString());
wrtr.WriteLine("TIPO="+d1["TIPO"].ToString());
wrtr.WriteLine("USER="+d1["USER"].ToString());
wrtr.WriteLine("ORDERS="+d2["EXP1"].ToString());
wrtr.Flush();
d1.Close();
}
else
{
wrtr.WriteLine("*****");
wrtr.Flush();
}
break;

case "ORDER":
try
{
// Read the incoming data
inLine=codes[1];

string sqla="INSERT INTO DELIVERY (ORDER, MOTIVACAO,DATAOCORRENCIA,
HORAOCORRENCIA,";
sqla+="COUNTY, TIPO_LOGRADOURO, LOGRADOURO, ";
sqla+="FLAGS, STATE_FLAGS, CITY_FLAGS, TIPO_ORDER, NOME_DELIVERY, ";
sqla+="PUT, RTJ, CNPJ, CPF, MARCA, MODELO, SPECIE, ANO, TIPO) VALUES
(";

string pvez="'";
while (inLine != null)
{
sqla+=pvez+inLine+"'";
inLine = rdr.ReadLine();

pvez=",'";
if (inLine=="*") break;
}
sqla+=")";
Trace.WriteLine(sqla);
string s1 = "insert into table1 (message) values ('"+sqla+"')";
OdbcCommand c1 = new OdbcCommand(s1,conn);
c1.ExecuteNonQuery();

rdr.Close();
wrtr.WriteLine("END");
wrtr.Flush();
//sw.Close();
}
catch (Exception ex)
{
string s = "insert into table1 (message) values ('"+ex.Message+"')";
OdbcCommand c=new OdbcCommand(s,conn);
c.ExecuteNonQuery();

}
break;
}
}

public bool IsReusable
{
get {return false;}
}

}

}




and then in the client form1 code. (need some changes in the design too.
that is, take off some components.


using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.IO;



namespace DeliverySmartClient
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox serverUrlTextbox;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.MainMenu mainMenu1;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.serverUrlTextbox = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
//
// serverUrlTextbox
//
this.serverUrlTextbox.Location = new System.Drawing.Point(16, 40);
this.serverUrlTextbox.Size = new System.Drawing.Size(208, 20);
this.serverUrlTextbox.Text =
"http://192.168.0.2/HandlerVdir/foo.delivery";
//
// label2
//
this.label2.Location = new System.Drawing.Point(24, 16);
this.label2.Text = "Server Message";
//
// button1
//
this.button1.Location = new System.Drawing.Point(80, 80);
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.Controls.Add(this.button1);
this.Controls.Add(this.label2);
this.Controls.Add(this.serverUrlTextbox);
this.Menu = this.mainMenu1;
this.MinimizeBox = false;
this.Text = "ASP.NET Handler Client";

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>

static void Main()
{
Application.Run(new Form1());
}



private void button1_Click(object sender, System.EventArgs e)
{

HttpWebRequest req1 =
(HttpWebRequest)WebRequest.Create(serverUrlTextbox.Text);
req1.Method = "PUT";
req1.AllowWriteStreamBuffering = true;
req1.KeepAlive=true;

// Retrieve request stream and wrap in StreamWriter
Stream reqS = req1.GetRequestStream();
StreamWriter wrtr1 = new StreamWriter(reqS);

wrtr1.WriteLine("ORDER=5555");
wrtr1.WriteLine("afd");
wrtr1.WriteLine(DateTime.Now.ToShortDateString());
wrtr1.WriteLine(DateTime.Now.ToShortTimeString());
wrtr1.WriteLine("afd");
wrtr1.WriteLine("afd");
wrtr1.WriteLine("LASTGOOD");
wrtr1.WriteLine("FIRSTERROR");
wrtr1.WriteLine("afdasdf");
wrtr1.WriteLine("afdasdf");
wrtr1.WriteLine("afdasdf");
wrtr1.WriteLine("afdasdf");
wrtr1.WriteLine("afdasdf");
wrtr1.WriteLine("afdasdf");
wrtr1.WriteLine("afdasdf");
wrtr1.WriteLine("afdasdf");
wrtr1.WriteLine("afdasdf");
wrtr1.WriteLine("afdasdf");
wrtr1.WriteLine("afdasdf");
wrtr1.WriteLine("afdasdf");
wrtr1.WriteLine("afdasdf");

wrtr1.WriteLine("*");

wrtr1.Flush();
wrtr1.Close();

// Submit the request and get the response object
HttpWebResponse resp1 = (HttpWebResponse) req1.GetResponse();

// Retrieve the response stream and wrap in a StreamReader
Stream respStream1 = resp1.GetResponseStream();
StreamReader rdr1 = new StreamReader(respStream1);
// Read through the response line-by-line
string inLine1 = rdr1.ReadLine();
rdr1.Close();


MessageBox.Show("Transmited with success!");
}
}
}



the problem is. when i execute the system as is, i got an error INTERNAL
SERVER ERROR. but if i take off part of the sqla string to have this:



string sqla="INSERT INTO DELIVERY (ORDER, MOTIVACAO,DATAOCORRENCIA,
HORAOCORRENCIA, COUNTY, TIPO_LOGRADOURO, LOGRADOURO) VALUES (";


and change the client to send only the lines to this sql, the code works.
any help will be welcome!

thx in advance

PS. you need to make the original project works first. then change it.
 

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