知识点

相关文章

更多

最近更新

更多

httpclient get请求

2019-03-16 22:32|来源: 网路

httpclient get进行get请求步骤:
1、创建Httpclient对象
HttpClients.createDefault();
2、创建http GET请求对象
new HttpGet(url);
3、执行请求
httpclient.execute(httpGet);
4、获取返回相关信息,如状态码、内容
response.getStatusLine().getStatusCode()
response.getEntity()
5、关闭Httpclient对象,相当于关闭浏览器


示例:

package com._656463.httpclient;
  
import java.io.File;
  
import org.apache.commons.io.FileUtils;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
  
public class DoGET {
    public static void main(String[] args) throws Exception {
    // 创建Httpclient对象
    CloseableHttpClient httpclient = HttpClients.createDefault();
    // 创建http GET请求
    HttpGet httpGet = new HttpGet("http://www.baidu.com/s?wd=java");
    CloseableHttpResponse response = null;
    try {
        // 执行请求
        response = httpclient.execute(httpGet);
        // 判断返回状是否为200
        if (response.getStatusLine().getStatusCode() == 200) {
        String content = EntityUtils.toString(response.getEntity(),"UTF-8");
        FileUtils.writeStringToFile(new File("E:\\baidu.html"),content, "UTF-8");
        System.out.println("内容长度=" + content.length());
        }
    } finally {
        if (response != null) {
        response.close();
        }
        // 相当于关闭浏览器
        httpclient.close();
    }
    }
}

整理于网络


相关问答

更多
  • Post请求是通过这种键值对参数的形式,添全URL那是get方式
  • 首先new一个LinkedBlockingQueue,然后有三个方法相应的去处理,一个是将所有post请求加入对列,即initPost,另一个是从取post请求getUrl,还有一个解出新的url并将形成新的post请求放入队列addUrl。然后new 几个线程就可以进行了,每个线程各司其职。注意不要用普通队列来放post请求,因为在在多线程下它的线程不安全,而linkedBlockingQueue是concurrent类库的 ,线程安全。
  • 先写到服务器本地临时文件,在传给远程服务器吧
  • 异步调用一个简单的方法是jms,比较通用的开源实现是activemq
  • 结帐http://htmlunit.sourceforge.net/ Checkout http://htmlunit.sourceforge.net/
  • 因此,这里发生的情况是您的客户端发送请求,没有及时得到响应,并因此再次重试相同的请求(因为它应该)。 这反过来又会导致多个POST请求被发送到您的服务器(几乎是连续发送),您的服务器目前无法正确处理这些请求。 要验证/调试,请尝试禁用HTTP重试,如下所示: defaultHttpClient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler ( ...
  • 如果你的xml是由java.lang.String编写的,你可以这样使用HttpClient public void post() throws Exception{ HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost("http://www.baidu.com"); String xml = "xxxx"; HttpE ...
  • Apache HttpClient不了解JSON的任何内容,因此您需要单独构造JSON。 为此,我建议您从json.org查看简单的JSON-java库。 (如果“JSON-java”不符合您的要求,json.org可以使用不同语言的图书馆列表。) 生成JSON后,您可以使用下面的代码来POST StringRequestEntity requestEntity = new StringRequestEntity( JSON_STRING, "application/json", " ...
  • 根据MSDN ,因为.NET 4.5以下实例方法是线程安全的 (感谢@ischell): CancelPendingRequests DeleteAsync GetAsync GetByteArrayAsync GetStreamAsync GetStringAsync PostAsync PutAsync SendAsync According to MSDN, since .NET 4.5 The following instance methods are thread safe (thanks @ ...
  • 您可以使用此代码前进,它适用于我.. URL url = new URL("Your URL"); HttpURLConnection httpsURLConnection = (HttpURLConnection)url.openConnection(); httpsURLConnection.setReadTimeout(15000); httpsURLConnection.setConnectTimeout(20000); httpsURLConnec ...