Android之用HTTP的get,post,HttpClient三种方式向service提交文本数据
2013-03-07 09:16
741 查看
/**
* HTTP请求
* @author kesenhoo
*
*/
public class HttpRequest
{
public static boolean sendXML(String path, String xml)throws Exception
{
byte[] data = xml.getBytes();
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST");
conn.setConnectTimeout(5 * 1000);
//如果通过post提交数据,必须设置允许对外输出数据
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");
conn.setRequestProperty("Content-Length", String.valueOf(data.length));
OutputStream outStream = conn.getOutputStream();
outStream.write(data);
outStream.flush();
outStream.close();
if(conn.getResponseCode()==200)
{
return true;
}
return false;
}
/**
* 通过get方式提交参数给服务器
* @param path
* @param params
* @param enc
* @return
* @throws Exception
*/
public static boolean sendGetRequest(String path, Map<String, String> params, String enc) throws Exception
{
//构造如下形式的字符串,这里的字符串依情况不同
// ?method=save&title=435435435&timelength=89&
//使用StringBuilder对象
StringBuilder sb = new StringBuilder(path);
sb.append('?');
//迭代Map
for(Map.Entry<String, String> entry : params.entrySet())
{
sb.append(entry.getKey()).append('=')
.append(URLEncoder.encode(entry.getValue(), enc)).append('&');
}
sb.deleteCharAt(sb.length()-1);
//打开链接
URL url = new URL(sb.toString());
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5 * 1000);
//如果请求响应码是200,则表示成功
if(conn.getResponseCode()==200)
{
return true;
}
return false;
}
/**
* 通过Post方式提交参数给服务器
* @param path
* @param params
* @param enc
* @return
* @throws Exception
*/
public static boolean sendPostRequest(String path, Map<String, String> params, String enc) throws Exception
{
//需要构造的字符串形式如下:
// title=dsfdsf&timelength=23&method=save
StringBuilder sb = new StringBuilder();
//如果参数不为空
if(params!=null && !params.isEmpty())
{
for(Map.Entry<String, String> entry : params.entrySet())
{
//Post方式提交参数的话,不能省略内容类型与长度
sb.append(entry.getKey()).append('=')
.append(URLEncoder.encode(entry.getValue(), enc)).append('&');
}
sb.deleteCharAt(sb.length()-1);
}
//得到实体的二进制数据
byte[] entitydata = sb.toString().getBytes();
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST");
conn.setConnectTimeout(5 * 1000);
//如果通过post提交数据,必须设置允许对外输出数据
conn.setDoOutput(true);
//这里只设置内容类型与内容长度的头字段
//内容类型Content-Type: application/x-www-form-urlencoded
//内容长度Content-Length: 38
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", String.valueOf(entitydata.length));
OutputStream outStream = conn.getOutputStream();
//把实体数据写入是输出流
outStream.write(entitydata);
//内存中的数据刷入
outStream.flush();
outStream.close();
//如果请求响应码是200,则表示成功
if(conn.getResponseCode()==200)
{
return true;
}
return false;
}
/**
* 在遇上HTTPS安全模式或者操作cookie的时候使用HTTPclient会方便很多
* 使用HTTPClient(开源项目)向服务器提交参数
* @param path
* @param params
* @param enc
* @return
* @throws Exception
*/
public static boolean sendRequestFromHttpClient(String path, Map<String, String> params, String enc) throws Exception
{
//需要把参数放到NameValuePair
List<NameValuePair> paramPairs = new ArrayList<NameValuePair>();
if(params!=null && !params.isEmpty())
{
for(Map.Entry<String, String> entry : params.entrySet())
{
paramPairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
}
//对请求参数进行编码,得到实体数据
UrlEncodedFormEntity entitydata = new UrlEncodedFormEntity(paramPairs, enc);
//构造一个请求路径
HttpPost post = new HttpPost(path);
//设置请求实体
post.setEntity(entitydata);
//浏览器对象
DefaultHttpClient client = new DefaultHttpClient();
//执行post请求
HttpResponse response = client.execute(post);
//从状态行中获取状态码,判断响应码是否符合要求
if(response.getStatusLine().getStatusCode()==200)
{
return true;
}
return false;
}
}
* HTTP请求
* @author kesenhoo
*
*/
public class HttpRequest
{
public static boolean sendXML(String path, String xml)throws Exception
{
byte[] data = xml.getBytes();
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST");
conn.setConnectTimeout(5 * 1000);
//如果通过post提交数据,必须设置允许对外输出数据
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");
conn.setRequestProperty("Content-Length", String.valueOf(data.length));
OutputStream outStream = conn.getOutputStream();
outStream.write(data);
outStream.flush();
outStream.close();
if(conn.getResponseCode()==200)
{
return true;
}
return false;
}
/**
* 通过get方式提交参数给服务器
* @param path
* @param params
* @param enc
* @return
* @throws Exception
*/
public static boolean sendGetRequest(String path, Map<String, String> params, String enc) throws Exception
{
//构造如下形式的字符串,这里的字符串依情况不同
// ?method=save&title=435435435&timelength=89&
//使用StringBuilder对象
StringBuilder sb = new StringBuilder(path);
sb.append('?');
//迭代Map
for(Map.Entry<String, String> entry : params.entrySet())
{
sb.append(entry.getKey()).append('=')
.append(URLEncoder.encode(entry.getValue(), enc)).append('&');
}
sb.deleteCharAt(sb.length()-1);
//打开链接
URL url = new URL(sb.toString());
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5 * 1000);
//如果请求响应码是200,则表示成功
if(conn.getResponseCode()==200)
{
return true;
}
return false;
}
/**
* 通过Post方式提交参数给服务器
* @param path
* @param params
* @param enc
* @return
* @throws Exception
*/
public static boolean sendPostRequest(String path, Map<String, String> params, String enc) throws Exception
{
//需要构造的字符串形式如下:
// title=dsfdsf&timelength=23&method=save
StringBuilder sb = new StringBuilder();
//如果参数不为空
if(params!=null && !params.isEmpty())
{
for(Map.Entry<String, String> entry : params.entrySet())
{
//Post方式提交参数的话,不能省略内容类型与长度
sb.append(entry.getKey()).append('=')
.append(URLEncoder.encode(entry.getValue(), enc)).append('&');
}
sb.deleteCharAt(sb.length()-1);
}
//得到实体的二进制数据
byte[] entitydata = sb.toString().getBytes();
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST");
conn.setConnectTimeout(5 * 1000);
//如果通过post提交数据,必须设置允许对外输出数据
conn.setDoOutput(true);
//这里只设置内容类型与内容长度的头字段
//内容类型Content-Type: application/x-www-form-urlencoded
//内容长度Content-Length: 38
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", String.valueOf(entitydata.length));
OutputStream outStream = conn.getOutputStream();
//把实体数据写入是输出流
outStream.write(entitydata);
//内存中的数据刷入
outStream.flush();
outStream.close();
//如果请求响应码是200,则表示成功
if(conn.getResponseCode()==200)
{
return true;
}
return false;
}
/**
* 在遇上HTTPS安全模式或者操作cookie的时候使用HTTPclient会方便很多
* 使用HTTPClient(开源项目)向服务器提交参数
* @param path
* @param params
* @param enc
* @return
* @throws Exception
*/
public static boolean sendRequestFromHttpClient(String path, Map<String, String> params, String enc) throws Exception
{
//需要把参数放到NameValuePair
List<NameValuePair> paramPairs = new ArrayList<NameValuePair>();
if(params!=null && !params.isEmpty())
{
for(Map.Entry<String, String> entry : params.entrySet())
{
paramPairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
}
//对请求参数进行编码,得到实体数据
UrlEncodedFormEntity entitydata = new UrlEncodedFormEntity(paramPairs, enc);
//构造一个请求路径
HttpPost post = new HttpPost(path);
//设置请求实体
post.setEntity(entitydata);
//浏览器对象
DefaultHttpClient client = new DefaultHttpClient();
//执行post请求
HttpResponse response = client.execute(post);
//从状态行中获取状态码,判断响应码是否符合要求
if(response.getStatusLine().getStatusCode()==200)
{
return true;
}
return false;
}
}
相关文章推荐
- Android之用HTTP的get,post,HttpClient三种方式向service提交文本数据
- Android之使用HTTP的get,post,HttpClient三种方式向服务器端提交文本数据
- Android----用HTTP的get,post,HttpClient三种方式向web service提交文本数据
- [Android开发]Android之使用HTTP的get,post,HttpClient三种方式向服务器端提交文本数据
- HTTP的get,post,HttpClient三种方式向服务器端提交文本数据
- Android网络数据之向服务器提交数据的三种方式(get+post+AsyncHttpClient)
- 使用get,post,httpclient三种方式向服务器提交文本数据
- 使用get,post,httpclient三种方式向服务器提交文本数据
- 使用get,post,httpclient三种方式向服务器提交文本数据
- Android - GET请求,POST请求,数据提交,编码,HttpClient,async-http开源框架,多线程下载,断点续传,xUtils框架
- Android 如何用HttpClient 以Post方式提交数据并添加http头信息
- Android Get,Post,AsyncHttpClient向服务器提交数据几种方式
- Android -- 提交数据到服务器,Get Post方式, 异步Http框架提交
- Android -- 提交数据到服务器,Get Post方式, 异步Http框架提交
- Android传统HTTP请求get----post方式提交数据(包含乱码问题)
- Android HttpClient框架get和post方式提交数据(非原创)
- Android 用HttpClient 以Post方式提交数据并添加http头信息
- Java基础知识强化之网络编程笔记18:Android网络通信之 使用HttpClient的Post / Get 方式读取网络数据(基于HTTP通信技术)
- Android Get,Post,AsyncHttpClient向服务器提交数据几种方式
- Android传统HTTP请求get----post方式提交数据(包括乱码问题)