Solr Change CommonsHttpSolrServer To HttpSolrServer

For basic authentication in solr 3.5, I use the following code,

String url = "http://192.168.192.11:8080/solr/FormResponses"; CommonsHttpSolrServer server = new CommonsHttpSolrServer( url ); String username = "user"; String password = "user123"; Credentials defaultcreds = new UsernamePasswordCredentials(username, password); server.getHttpClient().getState().setCredentials(AuthScope.ANY, defaultcreds); server.getHttpClient().getParams().setAuthenticationPreemptive(true); 

In CommonsHttpSolrServer 4.0 CommonsHttpSolrServer not available, so I want to replace it with HttpSolrServer . Can someone help me fix this?

+4
source share
4 answers

Finally, I find the answer myself,

 String url = "http://192.168.192.11:8080/solr/FormResponses"; DefaultHttpClient httpclient = new DefaultHttpClient(); httpclient.getCredentialsProvider().setCredentials( AuthScope.ANY, new UsernamePasswordCredentials("user", "user123")); SolrServer solrServer = new HttpSolrServer(url, httpclient); 
+2
source

Change the code as follows:

 import org.apache.http.auth.AuthScope; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.solr.client.solrj.impl.HttpSolrServer; String url = "http://192.168.192.11:8080/solr/FormResponses"; HttpSolrServer server = new HttpSolrServer( url ); DefaultHttpClient client = (DefaultHttpClient) server.getHttpClient(); UsernamePasswordCredentials defaultcreds = new UsernamePasswordCredentials("user", "user123"); client.getCredentialsProvider().setCredentials(AuthScope.ANY, defaultcreds); 

For server.getHttpClient().getParams().setAuthenticationPreemptive(true) in HttpClient 4 you can use the solution described here .

+7
source

You need to add the JAR solr-solrj-4.0.0.jar for the HttpClientUtil.

Then use below code:

 HttpSolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/"+url); HttpClientUtil.setBasicAuth((DefaultHttpClient) solrServer.getHttpClient(), "USERNAME", "PASSWORD"); 

It worked for me.

+1
source

This is the only way that works for me:

 String url = "192.168.192.11:8080/solr/FormResponses"; String username = "user"; String password = "user123"; HttpSolrServer server = new HttpSolrServer("http://" + username + ":" + password + "@" + url); 
+1
source

All Articles