try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(getSaveReplyBBSPostsUrl(bbsPostsUrl));
String authStr = replyBBSName + ":" + replyBBSPassword;
Base64 base64 = new Base64(0,null);
String encoding = base64.encodeToString(authStr.getBytes());
httpPost.setHeader("Authorization", "Basic " + encoding);
httpPost.addHeader("Accept", "application/json");
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("replierId", userId));
nameValuePairs.add(new BasicNameValuePair("content", content));
nameValuePairs.add(new BasicNameValuePair("ip", ipAddress));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
HttpResponse response = httpClient.execute(httpPost);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
log.info("reply bbs posts success.");
return;
}
HttpEntity httpEntity = response.getEntity();
String errorContent = EntityUtils.toString(httpEntity);
EntityUtils.consume(httpEntity);
log.error("reply bbs posts error, status code:#0,message:#1", response.getStatusLine().getStatusCode(), errorContent);
} catch (IOException e) {
log.error("reply bbs posts error message:#0", e.getMessage());
}
分享到:
相关推荐
本压缩包文件"rookie_httppost"提供了一个关于如何使用Java实现HttpPost请求的实例,这对于初学者或者开发者来说是一份非常实用的参考资料。 首先,我们来理解HttpPost请求的基本概念。HTTP协议定义了多种请求方法...
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.impl.client.DefaultHttpClient; ...
HttpPost httpPost = new HttpPost("http://你的服务器IP/你的接口"); ``` 3. **设置请求参数**: 如果需要传递参数,可以使用`NameValuePair`或`BasicNameValuePair`,然后通过`UrlEncodedFormEntity`将其转化...
接着,创建HttpPost对象,设置请求URL,并通过HttpEntity和NameValuePair构建POST数据: ```java HttpPost httpPost = new HttpPost("http://yourserver.com/endpoint"); List<NameValuePair> params = new ...
HttpPost httpPost = new HttpPost(urlAddress); // 设置请求参数 List<NameValuePair> formParams = new ArrayList(); formParams.add(new BasicNameValuePair("username", "yourUsername")); formParams.add(new ...
HttpPost httpPost = new HttpPost("http://example.com/api"); ``` 接下来,如果需要发送的数据是键值对形式,我们可以使用`UrlEncodedFormEntity`来构建请求体。例如: ```java List<NameValuePair> ...
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.impl.client.DefaultHttpClient; ...
HttpPost httpPost = new HttpPost("http://example.com"); List<NameValuePair> params = new ArrayList(); params.add(new BasicNameValuePair("key", "value")); httpPost.setEntity(new ...
HttpPost httpPost = new HttpPost("http://example.com/api/data"); httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded"); ``` 3. 添加请求参数: 数据通常以键值对的形式添加到POST请求中...
除了创建`HttpClient`和`HttpPost`对象外,还需要创建一个`List<NameValuePair>`来存储表单参数,然后将其封装到`UrlEncodedFormEntity`中,再设置到`HttpPost`对象上。 ##### 示例代码: ```java public static ...
HttpPost httpPost = new HttpPost(URL); // 添加POST参数(如果需要的话) List<NameValuePair> params = new ArrayList(); params.add(new BasicNameValuePair("key1", "value1")); params.add(new ...
以表单数据为例,我们可以使用`NameValuePair`来构造参数,并通过`setEntity`方法添加到`HttpPost`中: ```java import org.apache.http.message.BasicNameValuePair; import org.apache.http.entity.StringEntity;...
> ★HttpPost cannot be resolved to a type > ★NameValuePair cannot be resolved to a type > ★HttpClient cannot be resolved to a type > ★Http cannot be resolved to a variable > ★...
`HttpPost`是Apache HTTP客户端库中的一个类,用于向指定URL发起POST请求。本文将详细介绍如何在Android应用中使用`HttpPost`向服务器发送请求,以及相关操作技巧。 首先,确保在Android项目中引入了Apache HTTP库...
Apache HttpClient库提供了`HttpClient`和相关类,如`HttpPost`,`NameValuePair`等。在你的代码中,确保已经引入了以下依赖: ```java import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; ...
2. 添加普通参数:使用`NameValuePair`类表示键值对,然后添加到`List<NameValuePair>`,通过`setEntity`方法设置到`HttpPost`对象中。例如: ```java List<NameValuePair> params = new ArrayList(); params.add...
import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.protocol.HTTP; ...
HttpPost httpPost = new HttpPost(reqURL); ``` 创建一个指向目标URL的POST请求。 7. **设置请求参数**: ```java List<NameValuePair> formParams = new ArrayList<NameValuePair>(); for (Map.Entry, ...
HttpPost httpPost = new HttpPost("http://example.com"); List<NameValuePair> params = new ArrayList(); params.add(new BasicNameValuePair("key1", "value1")); params.add(new BasicNameValuePair("key2...