收藏
回答

小程序,获取OpenID失败

问题模块
API和组件

在小程序里面获得Code后,传到服务端进一步获取OpenID,但报告"Unable to connect to the remote server"错误。

C#代码如下:


 string strUrlString = " https://api.weixin.qq.com/sns/jscode2session?appid={0}&secret={1}&js_code={2}&grant_type=authorization_code";
string url = string.Format(strUrlString, strAppID_Xiao, strAppSecret_Xiao, code);
GetUsersHelper GetUsersHelper = new GetUsersHelper();
string RequestObj = GetUsersHelper.WXWebRequest(url, string.Empty);

……


WXWebRequest方法代码如下:

public string WXWebRequest(string posturl, string postData)
          {
               Stream outstream = null;
               Stream instream = null;
               StreamReader sr = null;
               HttpWebResponse response = null;
               HttpWebRequest request = null;
               Encoding encoding = Encoding.UTF8;

               Log log= new Log(AppDomain.CurrentDomain.BaseDirectory + @"/log/Log.txt");

               byte[] data = encoding.GetBytes(postData);
               // 准备请求
               try
               {
                    // 设置参数
                    request = WebRequest.Create(posturl) as HttpWebRequest;
                    CookieContainer cookieContainer = new CookieContainer();
                    request.CookieContainer = cookieContainer;
                    request.AllowAutoRedirect = true;
                    request.Method = "POST";
                    request.ContentType = "application/x-www-form-urlencoded";
                    request.ContentLength = data.Length;
                    outstream = request.GetRequestStream();
                    outstream.Write(data, 0, data.Length);
                    outstream.Close();

                    System.Net.ServicePointManager.DefaultConnectionLimit = 50;
                    //发送请求并获取相应回应数据
                    response = request.GetResponse() as HttpWebResponse;
                    //直到request.GetResponse()程序才开始向目标网页发送Post请求
                    instream = response.GetResponseStream();
                    sr = new StreamReader(instream, encoding);
                    //返回结果网页(html)代码
                    string content = sr.ReadToEnd();
                    string err = string.Empty;

                    response.Close();
                    response = null;

                    request = null;
                    return content;
               }
               catch (Exception ex)
               {
                    log.WriteLog("/********WXWebRequest出现错误,错误信息如下: ********/ ");
                    log.WriteLog("Class       :" + ex.TargetSite.DeclaringType.Name);
                    log.WriteLog("Method      :" + ex.TargetSite.Name);
                    log.WriteLog("ErrorMessage:" + ex.Message);
                    log.WriteLog("/**************************************************************/");
                    return string.Empty;
               }

         }


服务器的IP已经添加到小程序绑定的那个公众号的IP白名单中去了。

请教问题出在哪里?

最后一次编辑于  2017-11-29
回答关注问题邀请回答
收藏