发布到 Foursquare API 失败
我一直在为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论