Bunix
asked on
Http server communication in C# usercontrols
Hey,
I'm writing a usercontrol in c# that I load in a html page using the object tag. The 'applet' has to communicate with the server using the http name-vairable construction (i.e. the server needs to recieve something like name='value' via the http protocol).
How do i do that?
TIA, me
I'm writing a usercontrol in c# that I load in a html page using the object tag. The 'applet' has to communicate with the server using the http name-vairable construction (i.e. the server needs to recieve something like name='value' via the http protocol).
How do i do that?
TIA, me
ASKER
Sorry, I'll ask the question in a different way....
In HTML/PHP/ASP programming you send variables to the server in your url (for example "...page.php?ThisIsTheVari able=AndTh isIsItsVal ue"). So the server recieves, on its (HTTP) internet port (port 80), a string with, amongst an url and other data, the variable and its value "ThisIsTheVariable=AndThis IsItsValue "....
I've already looked at the WebRequest and the WebResponse classes, and I think it can do the job... but how? It can't be hard sending a string over the HTML-port to the server, so I hope it can be done within a few lines of code.
TIA, Bunix
P.S. All the code has to be within the UserControl, so the above solution with the code in the HTML page isn't usable.
In HTML/PHP/ASP programming you send variables to the server in your url (for example "...page.php?ThisIsTheVari
I've already looked at the WebRequest and the WebResponse classes, and I think it can do the job... but how? It can't be hard sending a string over the HTML-port to the server, so I hope it can be done within a few lines of code.
TIA, Bunix
P.S. All the code has to be within the UserControl, so the above solution with the code in the HTML page isn't usable.
The code above is used to request and recieve a picture - BUT it can be adapted to recieve ANY data that can be serialised, which is pretty much anything, somethings just require a bit of work.
The databinding in the <object> tag just passes a string to the ImagePath property of the UserControl. The property set a internal variable (imagePath) to the passed value. So the control now knows where to request its image. For some reason you have dismissed passing a value this way, so you will have to hard code you request path in your get data function.
A WebRequest is based on a URI request so it can request anything from the server, the only restriction on it is that it must request it from the same server that sent the hosting page.
The request uri( imagePath in my example ) for you could be "www.yourserver.co.uk/variables.txt" and the web server will send that file back.
More usefuly the WebRequest URI could be "www.yourserver.co.uk/variables.aspx" ( or *.php or *.asp or other dynamic );
In the Variables.aspx Prerender handler;
Response.Clear();
Response.Write("ThisIsTheV ariable=An dThisIsIts Value,This IsTheVaria ble2=AndTh isIsItsVal ue2";
inyour control
private bool GetVarsFromWebHost()
{
WebRequest wr = WebRequest.Create( "www.yourserver.co.uk/variables.aspx" );
wr.Credentials = System.Net.CredentialCache .DefaultCr edentials ;
wr.Timeout = 5000 ;
WebResponse wrr = null;
try
{
wrr = wr.GetResponse() ;
}
catch( Exception ex )
{
return false;
}
Stream s = wrr.GetResponseStream() ;
// s will now contain "ThisIsTheVariable=AndThis IsItsValue ,ThisIsThe Variable2= AndThisIsI tsValue2"
// which just leaves you the nasty business of parsing the string in variables or whatever...
// Look into SERIALIZATION in will make you coding life so much easier and it is simple to do....
}
The databinding in the <object> tag just passes a string to the ImagePath property of the UserControl. The property set a internal variable (imagePath) to the passed value. So the control now knows where to request its image. For some reason you have dismissed passing a value this way, so you will have to hard code you request path in your get data function.
A WebRequest is based on a URI request so it can request anything from the server, the only restriction on it is that it must request it from the same server that sent the hosting page.
The request uri( imagePath in my example ) for you could be "www.yourserver.co.uk/variables.txt" and the web server will send that file back.
More usefuly the WebRequest URI could be "www.yourserver.co.uk/variables.aspx" ( or *.php or *.asp or other dynamic );
In the Variables.aspx Prerender handler;
Response.Clear();
Response.Write("ThisIsTheV
inyour control
private bool GetVarsFromWebHost()
{
WebRequest wr = WebRequest.Create( "www.yourserver.co.uk/variables.aspx" );
wr.Credentials = System.Net.CredentialCache
wr.Timeout = 5000 ;
WebResponse wrr = null;
try
{
wrr = wr.GetResponse() ;
}
catch( Exception ex )
{
return false;
}
Stream s = wrr.GetResponseStream() ;
// s will now contain "ThisIsTheVariable=AndThis
// which just leaves you the nasty business of parsing the string in variables or whatever...
// Look into SERIALIZATION in will make you coding life so much easier and it is simple to do....
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
but two methods i use a lot to pass infomation from server to client usercontrol are:-
on the hosting page, use databing to pass information.
<object classid="yourControls.dll#
<param name="ImagePath" value='<%# Request.Url.AbsoluteUri.Re
</object>
Tthe param ImagePath matches up with a public property of the usercontrol named the same, the property HAS to be get and set even if nothing is expected to publicly access the property or dllhost will not be able to find it :-
public string ImagePath
{
get
{
return imagePath ;
}
set
{
imagePath = value ;
}
}
within the actual user control you can use the Webrequest and webresponse classes to retrieve data/infomation from the server, this code request a graphic but it could just as easly be a Serialised class/struc or other file, for LARGE files then I seggest you look at asynchrons versions of the GetResponse methods ( BeginGetRequest / BeginGetResponse etc )
private bool GetImageFromWebHost()
{
if( imagePath == null ) return false ;
WebRequest wr = WebRequest.Create( imagePath );
wr.Credentials = System.Net.CredentialCache
wr.Timeout = 5000 ;
WebResponse wrr = null;
try
{
wrr = wr.GetResponse() ;
}
catch( Exception ex )
{
return false;
}
Stream s = wrr.GetResponseStream() ;
Bitmap img = new Bitmap( s ) ;
graphImage = new Bitmap( img ) ;
Graphics g = Graphics.FromImage( graphImage ) ;
g.DrawImageUnscaled( img , 0 , 0 ) ;
g.Dispose() ;
img.Dispose() ;
img = null ;
s.Close() ;
return true;
}