dopost 与 doget 的区别 RT这两个方法有什么区别啊 。。谢谢大家。。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 在Form里面,可以使用post也可以使用get。它们都是method的合法取值。但是,post和get方法在使用上至少有以下几点不同:1、Get方法通过URL请求来传递用户的输入。Get方法传递的参数和值在URL上用?name=value&name=value的形式显示。Post方法通过另外的形式。2、通过get方法提交数据,可能会带来安全性的问题。比如一个登陆页面。当通过get方法提交数据时,用户名和密码将出现在URL上。如果登陆页面可以被浏览器缓存或其他人可以访问客户的这台机器。那么,别人即可以从浏览器的历史记录中,读取到此客户的账号和密码。所以,在某些情况下,get方法会带来严重的安全性问题。 建议在Form中,建议使用post方法。3、显然Get方法局限性在于当要传的值很多是,就不便这样,否则URL可能超长度而出错。 在web服务器传递请求信息时将web服务器传送url的编码格式和jsp页面、数据库等的编码格式保持一致就不会乱码,一般都设置为UTF-8! 乱码和get post方式没有关系,在于你的编码格式 public static String f_httpGetData(String Url,int port,boolean isSSL) throws Exception { HttpParams params = new BasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, Usual.mUTF8Name); HttpConnectionParams.setConnectionTimeout(params, Usual.mUrlConTime); HttpClient httpclient = new DefaultHttpClient(params); if(isSSL) { SSLContext sslcontext=f_createSimSSLContext(); SSLSocketFactory socketFactory = new SSLSocketFactory(sslcontext); socketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); if(port<=0) { port=443; } Scheme sch = new Scheme("https", socketFactory,port); httpclient.getConnectionManager().getSchemeRegistry().register(sch); } else { PlainSocketFactory plainFactory =PlainSocketFactory.getSocketFactory(); if(port<=0) { port=80; } Scheme sch = new Scheme("http", plainFactory,port); httpclient.getConnectionManager().getSchemeRegistry().register(sch); } HttpGet obj = new HttpGet(Url); HttpResponse response = httpclient.execute(obj); //处理返回状态 int sc = response.getStatusLine().getStatusCode(); StringBuilder rsb=new StringBuilder(); //如果Http返回状态为200 // Get hold of the response entity HttpEntity entity = response.getEntity(); if (entity != null) { BufferedReader reader = new BufferedReader( new InputStreamReader(entity.getContent(),Usual.mCharset_utf8)); try { String lineStr; while ((lineStr = reader.readLine()) != null) { rsb.append(lineStr); } } catch (IOException ex) { throw ex; } catch (RuntimeException ex) { obj.abort(); throw ex; } finally { if(reader!=null) { reader.close(); reader=null; } } if(sc!= HttpStatus.SC_OK) { throw new IOException("HttpStatus异常:"+sc+","+rsb.toString()); } } //关闭连接 httpclient.getConnectionManager().shutdown(); return rsb.toString(); }public static String f_httpPostData(String Url,int port,StringBuilder datasb, boolean isSSL,String contextType,String contentEncoding) throws Exception { if(datasb.length()==0) { return Usual.mEmpty; } HttpParams params = new BasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, Usual.mUTF8Name); HttpConnectionParams.setConnectionTimeout(params,Usual.mUrlConTime); HttpClient httpclient = new DefaultHttpClient(params); if(isSSL) { SSLContext sslcontext=f_createSimSSLContext(); SSLSocketFactory socketFactory = new SSLSocketFactory(sslcontext); socketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); if(port<=0) { port=443; } Scheme sch = new Scheme("https", socketFactory,port); httpclient.getConnectionManager().getSchemeRegistry().register(sch); } else { PlainSocketFactory plainFactory =PlainSocketFactory.getSocketFactory(); if(port<=0) { port=80; } Scheme sch = new Scheme("http", plainFactory,port); httpclient.getConnectionManager().getSchemeRegistry().register(sch); } //Post调用 HttpPost obj = new HttpPost(Url); StringEntity strEntity=new StringEntity(datasb.toString()); if(Usual.f_isNullOrEmpty(contextType)) { strEntity.setContentType("text/plain"); } else { //application/xml strEntity.setContentType(contextType); } if(Usual.f_isNullOrEmpty(contentEncoding)) { strEntity.setContentEncoding(Usual.mUTF8Name); } else { strEntity.setContentEncoding(contentEncoding); } obj.setEntity(strEntity); //重试次数 HttpRequestRetryHandler myRetryHandler = new HttpRequestRetryHandler() { public boolean retryRequest(IOException exception,int executionCount,HttpContext context) { //重试3次后取消 if (executionCount > 4) { // Do not retry if over max retry count return false; } if (exception instanceof NoHttpResponseException) { // Retry if the server dropped connection on us return true; } if (exception instanceof SSLHandshakeException) { // Do not retry on SSL handshake exception return false; } HttpRequest request = (HttpRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST); boolean idempotent = !(request instanceof HttpEntityEnclosingRequest); if (idempotent) { // Retry if the request is considered idempotent return true; } return false; } }; ((AbstractHttpClient)httpclient).setHttpRequestRetryHandler(myRetryHandler); //执行连接 HttpResponse response = httpclient.execute(obj); //处理返回状态 int sc = response.getStatusLine().getStatusCode(); StringBuilder rsb=new StringBuilder(); // Get hold of the response entity HttpEntity entity = response.getEntity(); if (entity != null) { BufferedReader reader = new BufferedReader( new InputStreamReader(entity.getContent(),Usual.mCharset_utf8)); try { String lineStr; while ((lineStr = reader.readLine()) != null) { rsb.append(lineStr); } } catch (IOException ex) { throw ex; } catch (RuntimeException ex) { obj.abort(); throw ex; } finally { if(reader!=null) { reader.close(); reader=null; } } //如果Http返回状态为200 if(sc!= HttpStatus.SC_OK) { throw new IOException("HttpStatus异常:"+sc+","+rsb.toString()); } } //关闭连接 httpclient.getConnectionManager().shutdown(); return rsb.toString(); } get方式传递中文参数是会出现乱码,对乱码的处理方式不是简单的设置setCharactorEncoding(utf-8)就能解决的,这个只是适合post方式,建议用post,如果非用用get的话,就这样处理接受到的参数String name = request.getParameter("name");将请求参数分解成字节数组byte[] bytes=name.getBytes("ISO-8859-1");将自己数组重新解码成字符串String name = new String (name,"utf-8"); InvocationTargetException异常 Field 'ip' doesn't have a default value 什么嘛. JSP提取数据库数据初始化下拉列表的错误! 我现在要如何再提高 jsp 通过连接打开新窗口时有时丢失session,请问这是怎么回事啊 jsp+sql server 2000 如何配置连接? 请问tomcat4.0以上支持Vector变量吗 请大侠帮忙,一个头痛的问题 高分请教大家一个struts里面checkbox的问题! 代码分享(springmvc,requirejs,less,maven) 想做个jsp项目 求思路 购物车的几个相关问题?
1、Get方法通过URL请求来传递用户的输入。Get方法传递的参数和值在URL上用?name=value&name=value的形式显示。Post方法通过另外的形式。
2、通过get方法提交数据,可能会带来安全性的问题。比如一个登陆页面。当通过get方法提交数据时,用户名和密码将出现在URL上。如果登陆页面可以被浏览器缓存或其他人可以访问客户的这台机器。那么,别人即可以从浏览器的历史记录中,读取到此客户的账号和密码。所以,在某些情况下,get方法会带来严重的安全性问题。 建议在Form中,建议使用post方法。
3、显然Get方法局限性在于当要传的值很多是,就不便这样,否则URL可能超长度而出错。
public static String f_httpGetData(String Url,int port,boolean isSSL)
throws Exception
{
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, Usual.mUTF8Name);
HttpConnectionParams.setConnectionTimeout(params, Usual.mUrlConTime);
HttpClient httpclient = new DefaultHttpClient(params);
if(isSSL)
{
SSLContext sslcontext=f_createSimSSLContext();
SSLSocketFactory socketFactory = new SSLSocketFactory(sslcontext);
socketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
if(port<=0)
{
port=443;
}
Scheme sch = new Scheme("https", socketFactory,port);
httpclient.getConnectionManager().getSchemeRegistry().register(sch);
}
else
{
PlainSocketFactory plainFactory =PlainSocketFactory.getSocketFactory();
if(port<=0)
{
port=80;
}
Scheme sch = new Scheme("http", plainFactory,port);
httpclient.getConnectionManager().getSchemeRegistry().register(sch);
}
HttpGet obj = new HttpGet(Url);
HttpResponse response = httpclient.execute(obj);
//处理返回状态
int sc = response.getStatusLine().getStatusCode();
StringBuilder rsb=new StringBuilder();
//如果Http返回状态为200
// Get hold of the response entity
HttpEntity entity = response.getEntity();
if (entity != null)
{
BufferedReader reader = new BufferedReader(
new InputStreamReader(entity.getContent(),Usual.mCharset_utf8));
try
{
String lineStr;
while ((lineStr = reader.readLine()) != null)
{
rsb.append(lineStr);
}
}
catch (IOException ex)
{
throw ex;
}
catch (RuntimeException ex)
{
obj.abort();
throw ex;
}
finally
{
if(reader!=null)
{
reader.close();
reader=null;
}
}
if(sc!= HttpStatus.SC_OK)
{
throw new IOException("HttpStatus异常:"+sc+","+rsb.toString());
}
}
//关闭连接
httpclient.getConnectionManager().shutdown();
return rsb.toString();
}public static String f_httpPostData(String Url,int port,StringBuilder datasb,
boolean isSSL,String contextType,String contentEncoding)
throws Exception
{
if(datasb.length()==0)
{
return Usual.mEmpty;
}
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, Usual.mUTF8Name);
HttpConnectionParams.setConnectionTimeout(params,Usual.mUrlConTime);
HttpClient httpclient = new DefaultHttpClient(params);
if(isSSL)
{
SSLContext sslcontext=f_createSimSSLContext();
SSLSocketFactory socketFactory = new SSLSocketFactory(sslcontext);
socketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
if(port<=0)
{
port=443;
}
Scheme sch = new Scheme("https", socketFactory,port);
httpclient.getConnectionManager().getSchemeRegistry().register(sch);
}
else
{
PlainSocketFactory plainFactory =PlainSocketFactory.getSocketFactory();
if(port<=0)
{
port=80;
}
Scheme sch = new Scheme("http", plainFactory,port);
httpclient.getConnectionManager().getSchemeRegistry().register(sch);
}
//Post调用
HttpPost obj = new HttpPost(Url);
StringEntity strEntity=new StringEntity(datasb.toString());
if(Usual.f_isNullOrEmpty(contextType))
{
strEntity.setContentType("text/plain");
}
else
{
//application/xml
strEntity.setContentType(contextType);
}
if(Usual.f_isNullOrEmpty(contentEncoding))
{
strEntity.setContentEncoding(Usual.mUTF8Name);
}
else
{
strEntity.setContentEncoding(contentEncoding);
}
obj.setEntity(strEntity);
//重试次数
HttpRequestRetryHandler myRetryHandler = new HttpRequestRetryHandler()
{
public boolean retryRequest(IOException exception,int executionCount,HttpContext context)
{
//重试3次后取消
if (executionCount > 4)
{
// Do not retry if over max retry count
return false;
}
if (exception instanceof NoHttpResponseException)
{
// Retry if the server dropped connection on us
return true;
}
if (exception instanceof SSLHandshakeException)
{
// Do not retry on SSL handshake exception
return false;
}
HttpRequest request = (HttpRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
if (idempotent)
{
// Retry if the request is considered idempotent
return true;
}
return false;
}
};
((AbstractHttpClient)httpclient).setHttpRequestRetryHandler(myRetryHandler);
//执行连接
HttpResponse response = httpclient.execute(obj);
//处理返回状态
int sc = response.getStatusLine().getStatusCode();
StringBuilder rsb=new StringBuilder();
// Get hold of the response entity
HttpEntity entity = response.getEntity();
if (entity != null)
{
BufferedReader reader = new BufferedReader(
new InputStreamReader(entity.getContent(),Usual.mCharset_utf8));
try
{
String lineStr;
while ((lineStr = reader.readLine()) != null)
{
rsb.append(lineStr);
}
}
catch (IOException ex)
{
throw ex;
}
catch (RuntimeException ex)
{
obj.abort();
throw ex;
}
finally
{
if(reader!=null)
{
reader.close();
reader=null;
}
}
//如果Http返回状态为200
if(sc!= HttpStatus.SC_OK)
{
throw new IOException("HttpStatus异常:"+sc+","+rsb.toString());
}
}
//关闭连接
httpclient.getConnectionManager().shutdown();
return rsb.toString();
}
如果非用用get的话,就这样处理接受到的参数
String name = request.getParameter("name");
将请求参数分解成字节数组
byte[] bytes=name.getBytes("ISO-8859-1");
将自己数组重新解码成字符串
String name = new String (name,"utf-8");