HttpClient发送POST请求的方法应该在ssm web项目中的哪一层,controller还是service;发送的路径怎么写?
可以写在jsp的form表单中啊 action="路径" ,method="post/get" 不是吗。
在service层
路径如下格式:http|https://ip|域名[:端口号]/项目名/后面省略
其中"|"代表或者,[]代表可能有也可能没有
public void post() {
try {
String url = "https://reg.163.com/logins.jsp";
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
List<NameValuePair> list = new ArrayList<NameValuePair>();
list.add(new BasicNameValuePair("password", "123456"));
list.add(new BasicNameValuePair("phone", "18215565210"));
list.add(new BasicNameValuePair("token", ""));
HttpEntity entity;
entity = new UrlEncodedFormEntity(list, "utf-8");
post.setEntity(entity);
HttpResponse response = client.execute(post);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entitys = response.getEntity();// 获得相应实体
String msg;
msg = EntityUtils.toString(entitys).trim();
postCon = msg;
Log.i("post成功--", msg);
handler.sendEmptyMessage(1);
} else {
Log.i("post失败--", "失败"
+ response.getStatusLine().getStatusCode());
handler.sendEmptyMessage(1);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}/* catch (JSONException e) {
e.printStackTrace();
}*/
}