request.BeginGetResponse(new AsyncCallback(ReadCallback

.Net技术 码拜 9年前 (2016-05-10) 853次浏览
//设置https验证方式
                if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
                {
                    ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);//验证服务器证书回调自动验证
                }
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri(url));
                request.ConnectionGroupName = uid + "," + utype;
                request.BeginGetResponse(new AsyncCallback(ReadCallback), request);
private static void ReadCallback(IAsyncResult ar)
        {
                HttpWebRequest request = (HttpWebRequest)ar.AsyncState;
                HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(ar);
                using (Stream responseStream = response.GetResponseStream())
                {
                    StreamReader readStream = new StreamReader(responseStream, Encoding.UTF8);
                    string unionid = readStream.ReadToEnd();
                    
                    if (!string.IsNullOrEmpty(unionid))
                    {
                        string[] arr = request.ConnectionGroupName.Split(",");
                        string uid = arr[0].ToString();
                        string utype = arr[1].ToString();
                        if (!string.IsNullOrEmpty(uid))
                        {
                            //更新数据库
                            if (utype == "1")
                            {
                                string filename = @"D:\www\2013_Tzrl\Tzrl_Wan\log.txt";
                                StreamWriter sr = null;
                                try
                                {
                                    if (!File.Exists(filename))
                                    {
                                        sr = File.CreateText(filename);
                                    }
                                    else
                                    {
                                        sr = File.AppendText(filename);
                                    }
                                    sr.WriteLine("," + uid + "," + utype + "," + unionid);
                                }
                                catch
                                {
                                }
                                finally
                                {
                                    if (sr != null)
                                        sr.Close();
                                }
                                //此处写入文件中的信息是正常回调过来的本人要的信息,但是等操作数据库更新的时候就无反应
//数据库操作是没有错的!本人测试过的。                             }
                            else
                            {
                            }
                        }
                    }
                    request.Abort();
                    response.Close();
                    readStream.Close();
                }
        }

哪位高手有遇到过相似的问题么?网上看到的也都是打印出来的,没有看到相似操作数据库更新的

解决方案

30

恭喜你答对了,这个应该只是异步回调的demo,不带数据库访问的,数据库写入的得你本人写,就按照一般的数据库访问代码写就是了。

40

会不会是原因是流之间的冲突,尝试:将回调信息保存到变量,然后关闭response、request、responseStream,然后建立sqlconnection,写数据库
再进一步确定写数据库有没有问题,例如,在那个代码位置,写入其他信息(如ABC,40等,本人编造的,不是回调返回),以确认是不是写入数据格式问题

30

引用:
Quote: 引用:

你所谓的数据库就是log.txt?

不是log.txt只是本人打印数据查看的!是可以正常,但mssql操作就不行

本人眼花了吗?哪来的sql操作?


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明request.BeginGetResponse(new AsyncCallback(ReadCallback
喜欢 (0)
[1034331897@qq.com]
分享 (0)