发布到 Foursquare API 失败

发布于 2024-11-28 11:55:23 字数 2927 浏览 1 评论 0原文

我一直在为 WIndows Mobile 6 开发 Foursquare 应用程序(因为我的合同还有至少 6 个月),我每次尝试 POST 数据(即签入)都失败了,所以它没有多大用处。关于为什么此代码总是在线失败的任何建议或指示

oOutStream = request.GetRequestStream();

- 请帮助。

        public HTTPPost(Uri Url, Dictionary<string, string> Parameters)
        {
            StringBuilder respBody = new StringBuilder();

            request = (HttpWebRequest)HttpWebRequest.Create(Url);
            request.UserAgent = "4SqLite 20110803";
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.Timeout = 12000000;
            string content = "?";
            foreach (string k in Parameters.Keys)
            {
                content += k + "=" + Parameters[k] + "&";
            }
            content = content.TrimEnd(new char[] { '&' });
            ASCIIEncoding encoding = new ASCIIEncoding();
            byte[] byte1 = encoding.GetBytes(content);
            request.ContentLength = byte1.Length;
            byte[] buf = new byte[8192];
            Stream oOutStream = null;
            int tmp = ServicePointManager.DefaultConnectionLimit;
            try
            {
                // send the Post
                oOutStream = request.GetRequestStream();
            }
            catch
            {
                MessageBox.Show("Oops! We couldn't send data to the Internet. Try again later.", "HttpPost: Request error",
                   MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
            }
            finally
            {
                if (oOutStream != null)
                {
                    oOutStream.Write(byte1, 0, byte1.Length);         //Send it
                    oOutStream.Close();
                }
            }
            try
            {
                // get the response
                response = (HttpWebResponse)request.GetResponse();

                Stream respStream = response.GetResponseStream();

                int count = 0;
                do
                {
                    count = respStream.Read(buf, 0, buf.Length);
                    if (count != 0)
                        respBody.Append(Encoding.ASCII.GetString(buf, 0, count));
                }
                while (count > 0);

                respStream.Close();
                ResponseBody = respBody.ToString();
                EscapedBody = GetEscapedBody();
            }
            catch
            {
                MessageBox.Show("Oops! We couldn't get data from the Internet. Try again later.", "HttpPost: Response error",
                   MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
            }
            finally
            {
                StatusCode = GetStatusLine();
                Headers = GetHeaders();

                response.Close();
            }

I have been working on a Foursquare app for WIndows Mobile 6 (as I still have at least 6months on my contract) I am failing on every attempt to POST data (ie Checkin) so it is not much use. Any suggestions or pointers on why this code always fails at the

oOutStream = request.GetRequestStream();

Line - HELP PLEASE.

        public HTTPPost(Uri Url, Dictionary<string, string> Parameters)
        {
            StringBuilder respBody = new StringBuilder();

            request = (HttpWebRequest)HttpWebRequest.Create(Url);
            request.UserAgent = "4SqLite 20110803";
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.Timeout = 12000000;
            string content = "?";
            foreach (string k in Parameters.Keys)
            {
                content += k + "=" + Parameters[k] + "&";
            }
            content = content.TrimEnd(new char[] { '&' });
            ASCIIEncoding encoding = new ASCIIEncoding();
            byte[] byte1 = encoding.GetBytes(content);
            request.ContentLength = byte1.Length;
            byte[] buf = new byte[8192];
            Stream oOutStream = null;
            int tmp = ServicePointManager.DefaultConnectionLimit;
            try
            {
                // send the Post
                oOutStream = request.GetRequestStream();
            }
            catch
            {
                MessageBox.Show("Oops! We couldn't send data to the Internet. Try again later.", "HttpPost: Request error",
                   MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
            }
            finally
            {
                if (oOutStream != null)
                {
                    oOutStream.Write(byte1, 0, byte1.Length);         //Send it
                    oOutStream.Close();
                }
            }
            try
            {
                // get the response
                response = (HttpWebResponse)request.GetResponse();

                Stream respStream = response.GetResponseStream();

                int count = 0;
                do
                {
                    count = respStream.Read(buf, 0, buf.Length);
                    if (count != 0)
                        respBody.Append(Encoding.ASCII.GetString(buf, 0, count));
                }
                while (count > 0);

                respStream.Close();
                ResponseBody = respBody.ToString();
                EscapedBody = GetEscapedBody();
            }
            catch
            {
                MessageBox.Show("Oops! We couldn't get data from the Internet. Try again later.", "HttpPost: Response error",
                   MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
            }
            finally
            {
                StatusCode = GetStatusLine();
                Headers = GetHeaders();

                response.Close();
            }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文