Solved

Http server communication in C# usercontrols

Posted on 2004-04-14
4
372 Views
Last Modified: 2012-08-14
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
0
Comment
Question by:Bunix
  • 3
4 Comments
 
LVL 5

Expert Comment

by:AdrianJMartin
ID: 10823709
Not quite sure what you mean by "http name-vairable construction .... via http protocol";

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#yourControlsNameSpace.yourControlsClassName" >
<param name="ImagePath"       value='<%# Request.Url.AbsoluteUri.Replace( "Start.aspx" , @"graphs/" ) + DataBinder.Eval(Container.DataItem, "ID") + ".png" %>' >
</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.DefaultCredentials ;
      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;

}
0
 

Author Comment

by:Bunix
ID: 10830946
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?ThisIsTheVariable=AndThisIsItsValue"). 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=AndThisIsItsValue"....

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.
0
 
LVL 5

Expert Comment

by:AdrianJMartin
ID: 10831179
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("ThisIsTheVariable=AndThisIsItsValue,ThisIsTheVariable2=AndThisIsItsValue2";

inyour control
private bool GetVarsFromWebHost()
{
     WebRequest wr = WebRequest.Create(  "www.yourserver.co.uk/variables.aspx" );

     wr.Credentials = System.Net.CredentialCache.DefaultCredentials ;
     wr.Timeout = 5000 ;

     WebResponse wrr = null;

     try
     {
     wrr = wr.GetResponse() ;
     }
     catch( Exception ex )
     {
          return false;
     }

     Stream s = wrr.GetResponseStream() ;

// s  will now contain "ThisIsTheVariable=AndThisIsItsValue,ThisIsTheVariable2=AndThisIsItsValue2"
// 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....





}
 










0
 
LVL 5

Accepted Solution

by:
AdrianJMartin earned 250 total points
ID: 10831189
the webrequest can also pass variables ie
"www.yourserver.co.uk/variables.aspx?var1=val1"
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question