Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Http server communication in C# usercontrols

Posted on 2004-04-14
4
Medium Priority
?
394 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 total points
ID: 10831189
the webrequest can also pass variables ie
"www.yourserver.co.uk/variables.aspx?var1=val1"
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

618 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