C # Call Java action Asynchronously

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 Microsoft. Phone. controls;
Using Microsoft. Internal;
Using system. xml;
Using system. IO;
Using system. text;
Using system. Threading;
Using system. net. browser;

Namespace panoramaapp2
{
Public partial class mainpage: phoneapplicationpage
{
Private Static string pmessage;
Public static manualresetevent alldone = new manualresetevent (false );

Public httpwebrequest objrequest = NULL;
Public static string myrequestdata = string. empty;

Public String urladdress = "http: // web address/client/login? Account = 1 & Password = 2 & phonetype = 3 & devicetoken = 1 ";

Public mainpage ()
{
Initializecomponent ();

// Set the data context of The ListBox control to the sample data
Datacontext = app. viewmodel;
This. Loaded + = new routedeventhandler (mainpage_loaded );

Dosend ();

}

Public void dosend ()
{
Stringbuilder completeurl = new stringbuilder (urladdress );
Objrequest = (httpwebrequest) webrequest. Create (urladdress. tostring ());
Objrequest. contenttype = "application/X-WWW-form-urlencoded ";
Objrequest. method = "Post ";
// Adding Headers
Objrequest. headers ["Header"] = "your value ";
Objrequest. headers ["content-language"] = "En-us ";

Myrequestdata = "your request data ";

// Begins the asynchronous request
Objrequest. begingetrequeststream (New asynccallback (getrequeststreamcallback), objrequest );
}

Private Static void getrequeststreamcallback (iasyncresult asyncresult)
{
Httpwebrequest objhttpwebrequest = (httpwebrequest) asyncresult. asyncstate;
// End the operation
Stream poststream = objhttpwebrequest. endgetrequeststream (asyncresult );
// Convert the string into a byte array.
Byte [] bytearray = encoding. utf8.getbytes (myrequestdata );
// Write to the request stream.
Poststream. Write (bytearray, 0, myrequestdata. Length );
Poststream. Close ();

// Start the asynchronous operation to get the response
Objhttpwebrequest. begingetresponse (New asynccallback (getresponsecallback), objhttpwebrequest );
// Request. begingetresponse (New asynccallback (getresponsecallback), objhttpwebrequest );
}

Private Static void getresponsecallback (iasyncresult asyncresult)
{
Httpwebrequest objhttpwebrequest = (httpwebrequest) asyncresult. asyncstate;
Httpwebresponse objhttpwebresponse = (httpwebresponse) objhttpwebrequest. endgetresponse (asyncresult );
Stream objstreamresponse = objhttpwebresponse. getresponsestream ();
Streamreader objstreamreader = new streamreader (objstreamresponse );
String responsestring = objstreamreader. readtoend (); // got response here
// MessageBox. Show ("response:" + responsestring );
// Close the Stream Object
Objstreamresponse. Close ();
Objstreamreader. Close ();
Objhttpwebresponse. Close ();
}
}
}

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.