request.getparameter
客户端发送请求代码片段:
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.Httpresponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.ssl.SSLsocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.Defaulthttpclient;
import org.apache.http.util.EntityUtils;、
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import java.util.Map;
import javax.net.ssl.SSLcontext;
import javax.net.ssl.TrustManager;
public Map HttpPost(String url, Map parameter) {
try {
org.apache.http.client.HttpClient httpClient = null;
if (StringUtils.isBlank(url)) {
throw new runtimeexception("url为空!");
}
if (url.trim().startsWith("https")) {
httpClient = getHttpsClient(new DefaultHttpClient());
} else {
httpClient = new DefaultHttpClient();
}
HttpResponse httpResponse = null;
HttpPost httpPost = new HttpPost(url);
//标准的表单提交
List list = new ArrayList();
list.add(new Basicnamevaluepair("key1", parameter.get("key1")));
list.add(new BasicNameValuePair("key2", parameter.get("key2")));
list.add(new BasicNameValuePair("requestData", parameter.get("requestData")));
httpPost.setEntity(new UrlEncodedFormEntity(list, "UTF-8"));
//判断是否使用代理
if ("YES".equals(isProxy)) {
int mid = proxyHost.indexof("|");
String proxyHostStr1 = "";
String proxyHostStr2 = "";
if (mid == -1) {
proxyHostStr1 = proxyHost;
} else {
proxyHostStr1 = proxyHost.substring(0, mid);
proxyHostStr2 = proxyHost.substring(mid + 1);
}
try {
HttpHost proxy = new HttpHost(proxyHostStr1, proxyPort);
httpClient.getparams().setParameter(
"http.route.default-proxy", proxy);
httpResponse = httpClient.execute(httpPost);
} catch (Exception e) {
HttpHost proxy = new HttpHost(proxyHostStr2, proxyPort);
httpClient.getParams().setParameter(
"http.route.default-proxy", proxy);
httpResponse = httpClient.execute(httpPost);
}
} else {
httpResponse = httpClient.execute(httpPost);
}
HttpEntity returnEntity = httpResponse.getEntity();
if (returnEntity != null) {
String resStr = EntityUtils.toString(returnEntity, responseCharSet);
return jacksonUtils.getMapFromJson(resStr);
}
} catch (Exception e) {
Log.ERROR("服务调用时出现异常", e);
}
return null;
}
private org.apache.http.client.HttpClient getHttpsClient(
org.apache.http.client.HttpClient httpclient) throws Exception {
SSLContext sslContext = SSLContext.getinstance("TLS");
sslContext.init(null, new TrustManager[] { tm }, null);
SSLSocketFactory sslSocketFactory = new SSLSocketFactory(sslContext,
SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
httpclient.getConnectionManager().getSchemeRegistry()
.register(new Scheme("https", 443, sslSocketFactory));
return httpclient;
}
服务端接受请求代码(采用原始servlet方式接受)
protected void doPost(HttpServletRequest request,
HttpServletResponse response) {
String serviceName = (String) request.getparameter("key的名称");
String responseJson = null;
/**
* 中间去做一些业务处理,把结果赋值给responseJson
*/
response.setcontenttype("APPlication/json");
response.setCharacterEncoding("UTF-8");
printwriter out = response.getWriter();
out.print(responseJson);
out.flush();
out.close();
}
上面的客户端和服务端通过的是 标准的表单提交的方式。这种方式才可以使用request.getParameter(“key的名称”)获取到值。
之前犯得错误是发送表单数据使用的是
StringEntity stringEntity=new StringEntity(request,requestCharSet);
httpPost.setEntity(stringEntity);
设置的表单数据,这种方式在服务端通过request.getParameter(“key的名称”)是根本获取不到数据的。这种方式发送的是个消息体,而且消息体是个字符串,不是表单的key:value 格式的,所以获取不到数据。这种方式发送的数据应该使用@Requestbody或者让Springmvc给你自动组装成参数 的方式去接受。
总结:request.getParameter(“key的名称”) 这种方式获取数据必须是标准的表单提交 方式才可以取到值。(注: application/x-www-form-urlencoded )
enctype=”application/x-www-form-urlencoded”是默认的编码方式,当以这种方式提交数据时Servlet的API提供了对这种编码方式解码的支持,只需要调用ServletRequest 类中的getParameter()方法就可以得到表单中提交的数据。
相关阅读
cursor属性:定义了鼠标指针放在一个元素边界范围内时所用的光标形状 设置不同的属性值会出现不同的效果: 文本:例如一段文字,鼠标放
在servlet 中使用 request.getSession().getServletCo
今天恰好有个上传文件的练习需要使用 request.getSession().getServletContext().getRealPath("upload/" ); 获取上传到服务器上
自己的云盘中有代码 文件名(通过ContentResolver获取本地的文件)http://blog.csdn.net/chay_chan/article/details/76984665
jquery select option 获取下拉框值改变时,选择不同值
有一些场景下,需要有这样一个功能:我们选择下拉框的不同值时,触发函数,该函数可能用到了我们选中的select的value,比如如:需要将该value
navigator.userAgent获取浏览器信息(类型及系统)
浏览器对于我们来说,可能是最熟悉的工具了。熟知的浏览器Firefox、Opera、Safari、IE、Chrome以外,据说世界上还有近百种浏览器。通