Webrequest. Web for data and communication

Source: Internet
Author: User

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. net;
Using system. windows;
Using system. Windows. controls;
Using system. Windows. documents;
Using system. Windows. input;
Using system. Windows. Media;
Using system. Windows. Media. animation;
Using system. Windows. shapes;
Using system. IO;

Namespace data and communication: webrequest
{
Public partial class mainpage: usercontrol
{
Public mainpage ()
{
Initializecomponent ();
}

Private void usercontrol_loaded (Object sender, routedeventargs E)
{
List <book> books = new list <book> (){
New Book ("professional ASP. NET 3.5 "),
New Book ("ASP. NET Ajax in action "),
New Book ("Silverlight in action "),
New Book ("ASP. NET 3.5 unleashed "),
New Book ("Introducing Microsoft ASP. NET Ajax ")
};

Books. itemssource = books;

}

Private string bookno;

Void books_selectionchanged (Object sender, selectionchangedeventargs E)
{
Bookno = books. selectedindex. tostring ();

Uri endpoint = new uri ("http: // localhost: 8675/bookhandler. ashx ");

Webrequest request = webrequest. Create (endpoint );
Request. method = "Post ";
Request. contenttype = "application/X-WWW-form-urlencoded ";
Request. begingetrequeststream (New asynccallback (requestready), request );
}

void requestready (iasyncresult asyncresult)
{< br> webrequest request = asyncresult. asyncstate as webrequest;
stream requeststream = request. endgetrequeststream (asyncresult);

using (streamwriter writer = new streamwriter (requeststream)
{< br> writer. write (string. format ("NO = {0}", bookno);
writer. flush ();
}< br> request. begingetresponse (New asynccallback (responseready), request);
}

Void responseready (iasyncresult asyncresult)
{
Webrequest request = asyncresult. asyncstate as webrequest;
Webresponse response = request. endgetresponse (asyncresult );

Using (Stream responsestream = response. getresponsestream ())
{
Streamreader reader = new streamreader (responsestream );
String price = reader. readtoend ();
If (this. lblprice. checkaccess ())
{
This. lblprice. Text = "Price:" + price;
}
Else
{
This. Dispatcher. begininvoke (New uicontextchangedeventhandler (uicontextchanged), price );
}
}
}
Public Delegate void uicontextchangedeventhandler (Object State );
Private void uicontextchanged (object state)
{
This. lblprice. Text = "Price:" + (string) State;
}
}
}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.