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
BunixAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
AdrianJMartinConnect With a Mentor Commented:
the webrequest can also pass variables ie
"www.yourserver.co.uk/variables.aspx?var1=val1"
0
 
AdrianJMartinCommented:
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
 
BunixAuthor Commented:
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
 
AdrianJMartinCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.