/// /// Define other methods and classes here /// /// /// /// public static Task MakeAsyncRequest(string contentType, string contenttxt, ... ...
/// <summary> /// Define other methods and classes here /// </summary> /// <param name="url"></param> /// <param name="contentType"></param> /// <returns></returns> public static Task<string> MakeAsyncRequest(string contentType, string contenttxt, string mobile) { string url = "http://www.dusystem.com/UserServiceAPI"; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.ContentType = contentType; request.Method = WebRequestMethods.Http.Post; //get request.Timeout = 20000; request.Proxy = null; byte[] data = System.Text.Encoding.GetEncoding("gbk").GetBytes("geovindu"); //System.Text.UnicodeEncoding.ASCII.GetBytes("geovindu");//UTF8 Base64Encoder myEncoder = new Base64Encoder(data); StringBuilder sb = new StringBuilder(); sb.Append(myEncoder.GetEncoded()); string msg = UrlEncode(contenttxt); string content = string.Format("method={0}&isLongSms={1}&username={2}&password={3}&smstype={4}&mobile={5}&content={6}", "MS", 0, "geovindu", sb.ToString(), 0, mobile, msg);// byte[] bytes = Encoding.GetEncoding("gbk").GetBytes(content); request.ContentLength = bytes.Length; Stream os = request.GetRequestStream(); //req.GetResponseAsync(); os.Write(bytes, 0, bytes.Length); os.Close(); //System.Net.WebResponse resp = req.GetResponse(); WebResponse resp = request.GetResponse(); Task<WebResponse> task = Task.Factory.FromAsync( request.BeginGetResponse, asyncResult => request.EndGetResponse(asyncResult), (object)null); return task.ContinueWith(t => ReadStreamFromResponse(resp, content)); } /// <summary> /// 對內容進行編碼 /// </summary> /// <param name="str"></param> /// <returns></returns> private static string UrlEncode(string str) { StringBuilder sb = new StringBuilder(); byte[] byStr = System.Text.Encoding.GetEncoding("gbk").GetBytes(str); //預設是System.Text.Encoding.Default.GetBytes(str) for (int i = 0; i < byStr.Length; i++) { sb.Append(@"%" + Convert.ToString(byStr[i], 16)); } return (sb.ToString()); } /// <summary> /// /// </summary> /// <param name="response"></param> /// <returns></returns> private static string ReadStreamFromResponse(WebResponse response, string content) { Stream responseStream = response.GetResponseStream(); using (StreamReader sr = new StreamReader(responseStream)) { string strContent = sr.ReadToEnd(); return strContent; } } delegate string SynchOperation(string value); /// <summary> /// /// </summary> /// <param name="callback"></param> /// <param name="value"></param> void BeginTheSynchronousOperation(AsyncCallback callback, string value) { SynchOperation op = new SynchOperation(SynchronousOperation); op.BeginInvoke(value, callback, op); } /// <summary> /// /// </summary> /// <param name="value"></param> /// <returns></returns> string SynchronousOperation(string value) { string str = "geovindu"; Thread.Sleep(10000); str = str + value; return str; } /// <summary> /// /// </summary> /// <param name="result"></param> void CallbackOperation(IAsyncResult result) { // get your delegate var ar = result.AsyncState as SynchOperation; // end invoke and get value var returned = ar.EndInvoke(result); Response.Write(returned); } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Button1_Click(object sender, EventArgs e) { BeginTheSynchronousOperation(CallbackOperation, this.TextBox1.Text.Trim()); var task = MakeAsyncRequest("application/x-www-form-urlencoded", "geovindu", "1388888888"); Response.Write(string.Format("Got response of {0}", task.Result)); //返回成功 }