Solr – 使用Httpclient实例化HttpSolrServer

我需要连接到代理服务器后面的solr服务器(?)。 以下我试过(没什么特别的):

SolrServer server = new HttpSolrServer("https://urltosolr/solr"); try { SolrPingResponse pingResponse = server.ping(); } catch (SolrServerException e) { .... } 

堆栈跟踪:

  org.apache.solr.client.solrj.SolrServerException: IOException occured when talking to server at: https://urltosolr/solr ... Caused by: javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated 

之后我尝试了HttpSolrServer的其他构造函数,但我不知道如何正确地将用户名和密码设置为HttpClient。 有人可以帮忙吗?

我没有看到您在代码中尝试通过代理连接的位置,您在创建HttpSolrServer时仅提供了solr url。 您可以在创建 HttpSolrServer实例时提供自己的HttpClient实例。 您的HttpClient实例可以包含有关代理的信息。 所需的代码应如下所示,您可以在http-components示例中找到 :

 DefaultHttpClient httpclient = new DefaultHttpClient(); HttpHost proxy = new HttpHost(proxyHost, proxyPort, "http"); httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); httpclient.getCredentialsProvider().setCredentials( AuthScope.ANY, new UsernamePasswordCredentials(username, password)); SolrServer solrServer = new HttpSolrServer(solrUrl, httpclient); 

更多地考虑您的问题,我认为您不需要使用代理连接。 你的错误是关于https。 看看这个其他问题和答案,看看你需要做什么。 您需要查看的httpclient版本是4.x.