HttpClient Post请求

doPost(null, "https://www.baidu.com/");

/**
* 访问数据库并返回JSON数据字符串
*
* @param params
* 向服务器端传的参数
* @param url
* @return
* @throws Exception
*/
public static String doPost(List<NameValuePair> params, String url)
throws Exception {
String result = null;
// 获取HttpClient对象
HttpClient httpClient = new DefaultHttpClient();
// 新建HttpPost对象
HttpPost httpPost = new HttpPost(url);
if (params != null) {
// 设置字符集
HttpEntity entity = new UrlEncodedFormEntity(params, HTTP.UTF_8);
// 设置参数实体
httpPost.setEntity(entity);
}
// 连接超时
httpClient.getParams().setParameter(
CoreConnectionPNames.CONNECTION_TIMEOUT, 3000);
// 请求超时
httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,
3000);
// 获取HttpResponse实例
HttpResponse httpResp = httpClient.execute(httpPost);
// 判断是够请求成功
if (httpResp.getStatusLine().getStatusCode() == 200) {
// 获取返回的数据
result = EntityUtils.toString(httpResp.getEntity(), "UTF-8");
} else {
result = null;
}
return result;

}

时间: 2024-10-10 14:23:26

HttpClient Post请求的相关文章

【黑马Android】(06)使用HttpClient方式请求网络/网易新闻案例

使用HttpClient方式请求网络 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"

java HttpClient POST请求

一个简单的HttpClient POST 请求实例 package com.httpclientget; import java.awt.List; import java.util.ArrayList; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost

java HttpClient GET请求

HttpClient GET请求小实例,先简单记录下. package com.httpclientget; import java.io.IOException; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient

用HttpPost 和 HttpClient 发送请求到web 端回调数据

btnok.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 跳转到新的页面 String name=usernameTextId.getText().toString(); String pwd=passwordTextId.getText().toString(); String url = "http://112.124.12.46/wxtest/login.soap?

httpclient + AsyncTask 请求数据 || httpclient + handler 请求数据

public class MyAsy extends AsyncTask<String, Integer, String> { private String json; @Override    protected String doInBackground(String... params) {        // TODO Auto-generated method stub        // 实例化HttpClient        HttpClient client = new De

httpclient post 请求

package com.thinkgem.jeesite.common.utils; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.ParseException; import org.apache.http.client.ClientProtocolException; imp

httpClient多线程请求

使用httpClient可模拟请求Url获取资源,使用单线程的请求速度上会有一定的限制,参考了Apache给出的例子,自己做了测试实现多线程并发请求,以下代码需要HttpClient 4.2的包,可以在http://hc.apache.org/downloads.cgi下载 1.并发请求 package generate.httpclient; import java.util.List; import java.util.concurrent.ExecutorService; import j

HttpClient实现请求

1 /// <summary> 2 /// HttpClient实现Get请求(异步) 3 /// </summary> 4 private static async void DoGet() 5 { 6 var url = "http://localhost:5555/api/Test/Get?id=1"; 7 //创建HttpClient(注意传入HttpClientHandler) 8 var handler = new HttpClientHandler

使用HttpClient实现请求转发功能

由于移动端要显示业务系统的数据,但是业务系统众多,如果直接和业务系统交互,会紧耦合,杂乱无章,不好管理,特编写请求转发组件,实现统一中转. 本文基于HttpClient实现了GET和POST两种方式: GET请求: /** * 根据GET方式请求获取请求内容 * @param url * @return */ private String getContentFromUrlByGet(String url) { String content = ""; try { HttpClient