知识点

相关文章

更多

最近更新

更多

httpclient post 请求

2019-03-16 23:18|来源: 网路

httpclient post请求与get请求的区别主要是httpclient.execute对象
httpclient post请求创建new HttpPost(String url);


示例:

package com._656463.httpclient;
  
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
  
public class DoPOST {
  
    public static void main(String[] args) throws Exception {
        // 创建Httpclient对象
        CloseableHttpClient httpclient = HttpClients.createDefault();
        // 创建http POST请求
        HttpPost httpPost = new HttpPost("http://www.iteye.com");
  
        CloseableHttpResponse response = null;
        try {
            // 执行请求
            response = httpclient.execute(httpPost);
            // 判断返回状态是否为200
            if (response.getStatusLine().getStatusCode() == 200) {
                String content = EntityUtils.toString(response.getEntity(),
                        "UTF-8");
                System.out.println(content);
            }
        } finally {
            if (response != null) {
                response.close();
            }
            httpclient.close();
        }
    }
}

本文参考网络


相关问答

更多
  • Post请求是通过这种键值对参数的形式,添全URL那是get方式
  • http请求传递的都是字符串。这种情况一般使用xml为信息载体。把java类转成xml, client得到xml再转成java类。 UserDTO user = new UserDTO(); ...//填充user XStream xstream = new XStream();//使用xstream转换pojo和xml字符串 // HttpPost连接...
  • 首先new一个LinkedBlockingQueue,然后有三个方法相应的去处理,一个是将所有post请求加入对列,即initPost,另一个是从取post请求getUrl,还有一个解出新的url并将形成新的post请求放入队列addUrl。然后new 几个线程就可以进行了,每个线程各司其职。注意不要用普通队列来放post请求,因为在在多线程下它的线程不安全,而linkedBlockingQueue是concurrent类库的 ,线程安全。
  • 这是httpclient的例子。 formparams 其实放的就是post的key和value。你用post的目的就是传这些东西。 先创建一个 post的对象(代码里没有,估计在上面),创建时应该已经设定了url了。 然后在 try中 将post的项目放入了post对象中,然后execute就访问url。 返回值在response中。这里是假设response必然返回了一个网页。它把网页打印了出来。
  • 你的这个问题就是web访问时的跨域问题。 关于跨域就一句话:同一个ip、同一个网络协议、同一个端口,三者都满足就是同一个域,否则就是跨域问题了。 下面给你介绍一个网站,一个大神关于跨域的问题解决,可以自己进去学习下。 http://blog.csdn.net/lovesummerforever/article/details/38052213
  • HttpPost httpPost = new HttpPost(hturl); List formparams = new ArrayList(); formparams.add(new BasicNameValuePair("name", "admin")); formparams.add(new BasicNameValuePair("password", "123456")); UrlEncodedFormEntity uefEntity;
  • 有时候我们在发送HTTP请求的时候会使用到POST方式,如果是传送普通的表单数据那将很方便,直接将参数到一个Key-value形式的Map 中即可。但是如果我们需要传送的参数是Json格式的,会稍微有点麻烦,我们可以使用HttpClient类库提供的功能来实现这个需求。假设我们需要发 送的数据是: { "blog": "http://www.iteblog.com", "Author": "iteblog" } 我们可以通过JSONObject够着Json: JSONObject jsonObject = ...
  • 好的,我自己解决了一些更多的调查。 我只需要添加一个将HTTP版本设置为1.1的参数,如下所示: HttpParams params = new BasicHttpParams(); params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); HttpClient httpclient = new DefaultHttpClient(params); 我发现这是由于非常好的HttpHelper类从书呆子和一 ...
  • Apache HttpClient不了解JSON的任何内容,因此您需要单独构造JSON。 为此,我建议您从json.org查看简单的JSON-java库。 (如果“JSON-java”不符合您的要求,json.org可以使用不同语言的图书馆列表。) 生成JSON后,您可以使用下面的代码来POST StringRequestEntity requestEntity = new StringRequestEntity( JSON_STRING, "application/json", " ...
  • 您正在使用的方法将第二个参数中的所有内容都用于HttpContent。 你可以在这里阅读更多。 你应该做的是将参数追加到myUri变量。 The method you are using takes everything in the 2nd parameter and uses it for HttpContent. You can read more here. What you should be doing is appending the parameters to myUri variable. ...