嘿 我正在为大学的最后一年项目开发应用程序,我正在努力尝试使用默认的http客户端来模仿下面的帖子,因为它看起来与我见过的其他人的格式不同。我使用的代码与此类似,并且已成功登录到网站:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.myurl.com/app/page.php");
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5);
nameValuePairs.add(new BasicNameValuePair("type", "20"));
nameValuePairs.add(new BasicNameValuePair("mob", "919895865899"));
nameValuePairs.add(new BasicNameValuePair("pack", "0"));
nameValuePairs.add(new BasicNameValuePair("exchk", "1"));
try {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
Log.d("myapp", "works till here. 2");
try {
HttpResponse response = httpclient.execute(httppost);
Log.d("myapp", "response " + response.getEntity());
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
但是,我正在努力将参数及其值与下面的帖子区分开来,并且我不确定号码8743499427392的用途。任何帮助将不胜感激:
(来自Http Live Headers的帖子的一部分)
http://messaging.o2online.ie/con_save.osp
POST /con_save.osp -----------------------------8743499427392
Content-Disposition: form-data; name="EContactID"
-1^
-----------------------------8743499427392
Content-Disposition: form-data; name="EContactIDList"
-----------------------------8743499427392
Content-Disposition: form-data; name="Categories"
Synch;
-----------------------------8743499427392
Content-Disposition: form-data; name="ENickName"
Test Colm Test Shannon
-----------------------------8743499427392
Content-Disposition: form-data; name="EAtt1"; filename=""
Content-Type: application/octet-stream
-----------------------------8743499427392
Content-Disposition: form-data; name="EPMobile"
0868617541
-----------------------------8743499427392
Content-Disposition: form-data; name="EMobile"
-----------------------------8743499427392--
答案 0 :(得分:2)
这称为多部分格式,这意味着每个表单字段都有自己的多部分请求块。此格式通常用于上传文件。
----- ######行只是将每个表单字段拆分为自己的部分的分隔符。确切的字符列在HTTP标头中,允许接收器解析主体。
您可以使用Fiddler(www.fiddler2.com)等工具查看现实世界网站上使用的此类格式。
另见: Multipart forms from C# client 和 Upload files with HTTPWebrequest (multipart/form-data)