【发布时间】:2013-11-30 00:23:28
【问题描述】:
我已经创建了我的 HTTPUrlConnection :
String postData = "x=val1&y=val2";
URL url = new URL(strURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Set-Cookie", sessionCookie);
conn.setRequestProperty("Content-Length", "" + Integer.toString(postData.getBytes().length));
// How to add postData as http body?
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
我不知道如何在 http 正文中设置 postData。怎么做?我最好改用HttpPost 吗?
感谢您的帮助。
【问题讨论】:
-
要发送Json数据吗?
-
@MaximShoustin 我不能简单地将它作为字符串发送吗?我通常在 iOS 中这样做:
NSString *string = @"x=val1&y=val2"; NSData *postData = [string dataUsingEncoding:NSISOLatin1StringEncoding allowLossyConversion:NO]; [request setHTTPBody:postData]; -
@Rob,你好,我想和你谈谈在 android 中向 POST 请求添加参数
标签: android httpurlconnection postdata