java ik分词 HttpResponseProxy{HTTP/1.1 401 Unauthorized

之前一直好好的,自从公司搬个家之后就不行了,通过url访问ik分词的时候会直接报401

		// 从客户端发送HTTP请求到服务器端,
		CloseableHttpClient httpClient = HttpClients.createDefault();
		String analyzer = "";
		int analyzerType = 0;
		if (analyzerType == 0) {
			analyzer = "ik_smart";
		} else {
			analyzer = "ik_max_word";
		}
		HttpPost httpPost = new HttpPost(Constant.SEARCH_URL
				+ "/omoappkeyword/_analyze?analyzer=" + analyzer
				+ "&pretty=true&text="
				+ URLEncoder.encode(queryString, "utf-8"));
        httpPost.addHeader("User-Agent", USER_AGENT);
		CloseableHttpResponse httpResponse = null;
		
		String enc = "username:password";
		httpPost.addHeader("Authorization", "Basic " + new Base64Encoder().encode(enc.getBytes()));

//		List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
//		urlParameters.add(new BasicNameValuePair("shanjin", "shanjin"));
//		HttpEntity postParams = new UrlEncodedFormEntity(urlParameters);
//		httpPost.setEntity(postParams);
		
		// 发送get请求
		httpResponse = httpClient.execute(httpPost, credentialContext);

其中

		String enc = "username:password";
		httpPost.addHeader("Authorization", "Basic " + new Base64Encoder().encode(enc.getBytes()));
是本次加进去的解决方法
		String enc = "username:password";
		httpPost.addHeader("Authorization", "Basic " + new Base64Encoder().encode(enc.getBytes()));

猜你喜欢

转载自blog.csdn.net/haiyang4988/article/details/77989170