2012-03-09 42 views
-1

我使用netbeans作为IDE给你一个背景的指示。Apache httpclient:教程坏了 - 如何实际使用这个库?

我正在玩Apache Apache httpclient库,就像我当前的应用程序一样,我在构建java HTTP连接时遇到了问题。

我听说apache库更强大。

无论如何,附带的Apache网站的HttpClient库教程文件似乎是有缺陷的:

HttpClient httpclient = new DefaultHttpClient(); 
HttpGet httpget = new HttpGet("http://localhost/"); 
HttpResponse response = httpclient.execute(httpget); 

净豆给了我这个代码段(从教程直复制)的问题。忘记本地主机的连起码的URI,与此出现的问题是:

  1. 的NetBeans抱怨的HttpClient和DefaultHttpClient是不兼容的类型。我可以解决这个看到的唯一方法是与投:

    (HttpClient) new DefaultHttpClient(); 
    
  2. Netbeans的抱怨说,因为“HTTPGET”这里是一个简单的方法,而不是一个“HttpUriRequest”的httpclient.execute()将抛出一个错误。

如何简单的3行教程如此错误,如果在这个例子中有太多的缺陷,我将如何成功地完成请求?

我迷路了,有人可以帮忙。似乎有几种不同的方式,所有这些都不是我要找的。

我希望能够在String中的应用中获取格式正确的URL,然后关注所有重定向。我对这个回应的内容不感兴趣,仅仅是它会下降的cookies。

感谢,

格雷戈里

+0

取决于您是否使用HttpClient 3.x或HttpClient 4.x?两者都不兼容。 – 2012-03-09 08:29:38

+0

4.x从http://hc.apache.org/下载并从http://hc.apache.org/httpcomponents-client-ga/tutorial/pdf/httpclient-tutorial.pdf下面的教程 – ortonomy 2012-03-09 08:44:53

+0

什么是错误抛出? – 2012-03-09 08:50:17

回答

1

我建议看你的进口。我认为NetBeans导入了您的HttpClient 3.x而不是4.x.尝试更正您的导入。

+0

好的,我在下载中找到了一些示例类文件。 你会发现这个有趣的 - 在它说的评论:@since 4.0,并提供此方法来实现连接: 'DefaultHttpClient client1 = new DefaultHttpClient(); HttpHost target = new HttpHost(“www.google.com”,80,“http”); HttpGet req = new HttpGet(“/”); HttpResponse rsp = client1.execute(target,req);' – ortonomy 2012-03-09 09:34:29

+0

但是,这段代码仍然不起作用。 Netbeans仍然抱怨它找不到符号 - 因为DefaultHttpClient没有“执行”方法。我认为你是正确的导入,但我使用的JAR文件,所以我有我需要的所有文件 - 他们都在课堂路径。你认为我可能会错过依赖关系吗? – ortonomy 2012-03-09 09:38:01

+0

好吧,我放弃了。 Netbeans不会让它运行,但是我尝试导入这些文件。我发现一些家伙Maven项目,它似乎工作,但我不知道如何使用maven或将他所做的工作转移到我的项目。这似乎已经枯竭了。无论如何,我想你的回答是正确的。感谢您的努力... – ortonomy 2012-03-09 13:45:36

0

您是否尝试过使用此代码,他们似乎比您做不同的机制。采取从here。这是为了3.X版本,但可能是因为你使用的是不同的版本。

import org.apache.commons.httpclient.*; 
import org.apache.commons.httpclient.methods.*; 
import org.apache.commons.httpclient.params.HttpMethodParams; 

import java.io.*; 

public class HttpClientTutorial { 

    private static String url = "http://www.apache.org/"; 

    public static void main(String[] args) { 
    // Create an instance of HttpClient. 
    HttpClient client = new HttpClient(); 

    // Create a method instance. 
    GetMethod method = new GetMethod(url); 

    // Provide custom retry handler is necessary 
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, 
      new DefaultHttpMethodRetryHandler(3, false)); 

    try { 
     // Execute the method. 
     int statusCode = client.executeMethod(method); 

     if (statusCode != HttpStatus.SC_OK) { 
     System.err.println("Method failed: " + method.getStatusLine()); 
     } 

     // Read the response body. 
     byte[] responseBody = method.getResponseBody(); 

     // Deal with the response. 
     // Use caution: ensure correct character encoding and is not binary data 
     System.out.println(new String(responseBody)); 

    } catch (HttpException e) { 
     System.err.println("Fatal protocol violation: " + e.getMessage()); 
     e.printStackTrace(); 
    } catch (IOException e) { 
     System.err.println("Fatal transport error: " + e.getMessage()); 
     e.printStackTrace(); 
    } finally { 
     // Release the connection. 
     method.releaseConnection(); 
    } 
    } 
} 
+0

我使用的版本是4.x直接从http://hc.apache.org/ 下载并在这里教程:http://hc.apache.org/httpcomponents -client-ga/tutorial/pdf/httpclient-tutorial.pdf 它似乎完全borked.But感谢代码片段,我会看看我能做些什么。 – ortonomy 2012-03-09 08:44:03

0

当我用这个(在Android)我实现了一个CustomHttpClient,下面的例子here

+0

我直接从apache下载了这个库,网址是http://hc.apache.org/ 我会看看如果我能遵循这个规则,我会很乐意真正地建立一个连接。 – ortonomy 2012-03-09 08:42:29

-1

非常有线。有同样的问题,我把httpclient 4.5.2这是最新版本httpclient 4.3.6和错误消失像魔术!