C # asynchronous call of Apsara WebService to receive SMS messages in real time

Source: Internet
Author: User

Step 1:

Create a new Win window project, and add web reference Feixin WebService, reference address: http://io.feirobot.cn/fWebSer.asmx? WSDL, rename reference to: fetionobj

Step 2:

Add the following controls to the main window:

1. Add the Textbox Control to display the received information. Name it showtxt. Set the control multiline to true to display multiple rows and drag them larger.

2. Add the Textbox Control to enter the logon number name: loginno.

3. Add the Textbox Control to enter the logon password name: loginpass.

4. Add a button control named login and set text = logon. The click event points to the login_click function.

Step 3:

Write relatedCode

1. Add reference at the top of the Code:

Using fetionobj. fwebser;

2. The following is the source code of asynchronous call:

Private string _ mymno = ""; // Save the upload number

Private fwebser FWS = NULL; // famous Interface Class

Private bool reasync = false; // enables or disables continuous receiving of information.

Public form1 ()
{
Initializecomponent ();

FWS = new fwebser (); // create a WebService Interface Class
FWS. cookiecontainer = new system. net. cookiecontainer (); // This sentence is very important; otherwise, you will not be able to log on
}

Public bool login_click (Object sender, eventargs E)
{
_ Mymno = loginno. text;


FWS. login (_ mymno, loginpass. Text); // you must first send the logon command
If (! Reasync) // The receiving has stopped before you can start asynchronous receiving again.
{
Reasync = true;
Delegatetrecom DN = new delegatetrecom (trecom); // instantiate the delegate and assign a value first
Iasyncresult IAR = DN. begininvoke (_ mymno, new asynccallback (callbacktrecom), DN); // start asynchronous execution
}
}

// multi-thread security delegate output
private delegate void mycontrol (control ACC, string txt);
private void setout (control YY, string txt)
{< br> If (ACC. invokerequired)
ACC. invoke (New mycontrol (setout), ACC, txt);
else
ACC. TEXT = TXT + "\ r \ n" + ACC. text;
}

Private delegate void delegatetrecom (string RECOM); // defines the delegate
Private void trecom (string mymno)
{// Function for processing received information asynchronously
Try
{
String remsg = FWS. remsg (mymno, 40); // receives data in persistent connection mode. If no data exists, you can wait up to 40 seconds before returning it.
String [] rearr = remsg. Split ('\ n'); // split event records
For (INT I = 0; I <rearr. length; I ++) // process all events received
{
// Analyze each event
String COM = "", rfno = "", rmno = "", MSG = "", errtxt = "", rgno = "", fname = "";
String [] comarr = rearr [I]. Split ('&');
For (Int J = 0; j <comarr. length; j ++)
{
String [] varr = comarr [J]. Split ('= ');
If (varr. Length <2) continue;
If (varr [0] = "com") COM = varr [1];
If (varr [0] = "rfno") rfno = varr [1];
If (varr [0] = "rmno") rmno = varr [1];
If (varr [0] = "MSG") MSG = varr [1];
If (varr [0] = "errtxt") errtxt = varr [1];
If (varr [0] = "rgno") rgno = varr [1];
If (varr [0] = "fname") fname = varr [1];
}
If (COM = "rmsg ")
{// Receive new message
Setout (showtxt, "received" + fname + "(" + rfno + "|" + rmno + ") new message:" + MSG );
}
If (COM = "sendok ")
{// Sent successfully

Setout (showtxt, "sent to" + rfno + "succeeded, content:" + MSG );
}
If (COM = "senderr ")
{// Failed to send
Setout (showtxt, "sent to" + rfno + "failed, error message:" + errtxt );
}
If (COM = "loginok ")
{// Logon successful

Setout (showtxt, "Login successful, your flying signal:" + rfno + ", mobile phone number:" + rmno + ", nickname:" + fname );
}
If (COM = "loginerr ")
{// Logon Failed
Reasync = false; // stop receiving events

setout (showtxt, "Logon Failed");
}< br> If (COM = "FADD ")
{// added as a friend

Setout (showtxt, "added as a friend, peer information:" + MSG. Trim ());
}
If (COM = "list ")
{// Friends list complete

Setout (showtxt, "friends list completed ");
}
If (COM = "logout ")
{// Deregistered
Reasync = false; // stop receiving events
Setout (showtxt, "logged out ");
}
If (COM = "changed ")
{// Friend status changed

String [] Farr = MSG. Trim (). Split ('| ');
If (Farr. length> = 7)
{
If (Farr [5] = "chat ")
{
Setout (showtxt, "friends open chat window:" + MSG. Trim ());
}
Else
{
Setout (showtxt, "Friend status changed:" + MSG. Trim ());
}
}
}
}
}
Catch (exception ER ){
Setout (showtxt, "receive error:" + Er. Message + Er. stacktrace );
}
If (reasync)
{// Repeatedly call to receive data
Thread. Sleep (2000); // control the call speed
Delegatetrecom DN = new delegatetrecom (trecom); // instantiate the delegate and assign a value first
Iasyncresult IAR = DN. begininvoke (mymno, new asynccallback (callbacktrecom), DN); // start asynchronous execution
}
}
// Method (callback method) to be executed when asynchronous completion is completed)

Private void callbacktrecom (iasyncresult AR)
{
// Obtain the delegate object from the asynchronous ar. asyncstate.
Delegatetrecom DN = (delegatetrecom) Ar. asyncstate;
// Be sure to endinvoke, otherwise...
DN. endinvoke (AR );
}

It is easy to call other functions such as sending information. You can add controls to implement them by yourself.

Original article: http://blog.csdn.net/mywwl/archive/2009/05/11/4169455.aspx

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.