`

httpPost NameValuePair

 
阅读更多
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());
        }
分享到:
评论

相关推荐

    httppost请求实例

    本压缩包文件"rookie_httppost"提供了一个关于如何使用Java实现HttpPost请求的实例,这对于初学者或者开发者来说是一份非常实用的参考资料。 首先,我们来理解HttpPost请求的基本概念。HTTP协议定义了多种请求方法...

    org.apache.http 相关的jar包

    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; ...

    android利用httppost实现和服务器的通讯

    HttpPost httpPost = new HttpPost("http://你的服务器IP/你的接口"); ``` 3. **设置请求参数**: 如果需要传递参数,可以使用`NameValuePair`或`BasicNameValuePair`,然后通过`UrlEncodedFormEntity`将其转化...

    android HttpPost请求连接服务器端的实例

    接着,创建HttpPost对象,设置请求URL,并通过HttpEntity和NameValuePair构建POST数据: ```java HttpPost httpPost = new HttpPost("http://yourserver.com/endpoint"); List&lt;NameValuePair&gt; params = new ...

    HTTP POST 提交方式

    HttpPost httpPost = new HttpPost(urlAddress); // 设置请求参数 List&lt;NameValuePair&gt; formParams = new ArrayList(); formParams.add(new BasicNameValuePair("username", "yourUsername")); formParams.add(new ...

    httpPost例子

    HttpPost httpPost = new HttpPost("http://example.com/api"); ``` 接下来,如果需要发送的数据是键值对形式,我们可以使用`UrlEncodedFormEntity`来构建请求体。例如: ```java List&lt;NameValuePair&gt; ...

    org.apache.http源代码和jar包

    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; ...

    HttpClient发送http请求(post和get)需要的jar包+内符java代码案例+注解详解

    HttpPost httpPost = new HttpPost("http://example.com"); List&lt;NameValuePair&gt; params = new ArrayList(); params.add(new BasicNameValuePair("key", "value")); httpPost.setEntity(new ...

    以Post方法传输数据_JAVA

    HttpPost httpPost = new HttpPost("http://example.com/api/data"); httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded"); ``` 3. 添加请求参数: 数据通常以键值对的形式添加到POST请求中...

    java HttpClient 发送GET请求和带有表单参数的POST请求教程例子

    除了创建`HttpClient`和`HttpPost`对象外,还需要创建一个`List&lt;NameValuePair&gt;`来存储表单参数,然后将其封装到`UrlEncodedFormEntity`中,再设置到`HttpPost`对象上。 ##### 示例代码: ```java public static ...

    http basic authentication通过post方式访问api示例分享 basic认证示例

    HttpPost httpPost = new HttpPost(URL); // 添加POST参数(如果需要的话) List&lt;NameValuePair&gt; params = new ArrayList(); params.add(new BasicNameValuePair("key1", "value1")); params.add(new ...

    AndroidHttpPost:Android Http Post 演示

    以表单数据为例,我们可以使用`NameValuePair`来构造参数,并通过`setEntity`方法添加到`HttpPost`中: ```java import org.apache.http.message.BasicNameValuePair; import org.apache.http.entity.StringEntity;...

    httpcomponents-client-4.5.5-bin和src_20180518.zip

    &gt; ★HttpPost cannot be resolved to a type &gt; ★NameValuePair cannot be resolved to a type &gt; ★HttpClient cannot be resolved to a type &gt; ★Http cannot be resolved to a variable &gt; ★...

    Android使用httpPost向服务器发送请求的方法

    `HttpPost`是Apache HTTP客户端库中的一个类,用于向指定URL发起POST请求。本文将详细介绍如何在Android应用中使用`HttpPost`向服务器发送请求,以及相关操作技巧。 首先,确保在Android项目中引入了Apache HTTP库...

    httpclient发送post请求

    Apache HttpClient库提供了`HttpClient`和相关类,如`HttpPost`,`NameValuePair`等。在你的代码中,确保已经引入了以下依赖: ```java import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; ...

    HttpClient完整实例(普通参数、JSON格式参数)

    2. 添加普通参数:使用`NameValuePair`类表示键值对,然后添加到`List&lt;NameValuePair&gt;`,通过`setEntity`方法设置到`HttpPost`对象中。例如: ```java List&lt;NameValuePair&gt; params = new ArrayList(); params.add...

    org.apache.http jar包

    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; ...

    https发送post请求

    HttpPost httpPost = new HttpPost(reqURL); ``` 创建一个指向目标URL的POST请求。 7. **设置请求参数**: ```java List&lt;NameValuePair&gt; formParams = new ArrayList&lt;NameValuePair&gt;(); for (Map.Entry, ...

    java_http_post.zip_http服务端_服务器接收http

    HttpPost httpPost = new HttpPost("http://example.com"); List&lt;NameValuePair&gt; params = new ArrayList(); params.add(new BasicNameValuePair("key1", "value1")); params.add(new BasicNameValuePair("key2...

Global site tag (gtag.js) - Google Analytics