为什么form表单提交的数据 后台只接收到了一个

如题所述

这里只介绍htt方面。网络请求方式可分为get请求,post两种请求方式,GET方式在进行数据请求时,会把数据附加到URL后面传递给服务器,比如常见的:http://XXX.XXX.XXX/XX.aspx?id=1,POST方式则是将请求的数据放到HTTP请求头中,作为请求头的一部分传入服务器。
所以,在进行HTTP编程前,首先要明确究竟使用的哪种方式进行数据请求的。

  android中Http编程有两种:1、HttpURLConnection;2、HttpClient

  首先介绍一下HttpURLConnection方式的get请求和post请求方法:

  [java] view
plaincopyprint?

  private Map<String, String> paramsValue;

  String urlPath=null;

  

  // 发送地http://192.168.100.91:8080/myweb/login?username=abc&password=123

  public void initData(){

  

  urlPath="http://192.168.100.91:8080/myweb/login";

  paramsValue=new HashMap<String, String>();

paramsValue.put("username", "111");

paramsValue.put("password", "222");

}

private Map<String, String> paramsValue;
String urlPath=null;

// 发送地http://192.168.100.91:8080/myweb/login?username=abc&password=123
public void initData(){

urlPath="http://192.168.100.91:8080/myweb/login";
paramsValue=new HashMap<String, String>();
paramsValue.put("username", "111");
paramsValue.put("password", "222");
}
  get方式发起请求:

  [java] view
plaincopyprint?

  private boolean sendGETRequest(String path, Map<String, String> params) throws Exception {

  boolean success=false;

  

  // StringBuilder是用来组拼请求地址和参数

  StringBuilder sb = new StringBuilder();

  sb.append(path).append("?");

  if (params != null && params.size() != 0) {

  for (Map.Entry<String, String> entry : params.entrySet()) {

  // 如果请求参数中有中文,需要进行URLEncoder编码 gbk/utf8

  sb.append(entry.getKey()).append("=").append(URLEncoder.encode(entry.getValue(), "utf-8"));

  sb.append("&");

  }

  sb.deleteCharAt(sb.length() - 1);

  }

  

  URL url = new URL(sb.toString());

  HttpURLConnection conn = (HttpURLConnection) url.openConnection();

  conn.setConnectTimeout(20000);

  conn.setRequestMethod("GET");
  if (conn.getResponseCode() == 200) {

  success= true;

  }

  if(conn!=null)

  conn.disconnect();

  return success;

  }
  private boolean sendGETRequest(String path, Map<String, String> params) throws Exception {
boolean success=false;

// StringBuilder是用来组拼请求地址和参数
StringBuilder sb = new StringBuilder();
sb.append(path).append("?");
if (params != null && params.size() != 0) {
for (Map.Entry<String, String> entry : params.entrySet()) {
// 如果请求参数中有中文,需要进行URLEncoder编码 gbk/utf8
sb.append(entry.getKey()).append("=").append(URLEncoder.encode(entry.getValue(), "utf-8"));
sb.append("&");
}
sb.deleteCharAt(sb.length() - 1);
}

URL url = new URL(sb.toString());
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(20000);
conn.setRequestMethod("GET");

if (conn.getResponseCode() == 200) {
success= true;
}
if(conn!=null)
conn.disconnect();
return success;
}
温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答