Solved

Http server communication in C# usercontrols

Posted on 2004-04-14
4
369 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now