我正在使用couchdb4j api通過會話對象與couchdb建立連接(版本0.11.2)。 Couchdb數據庫使用用戶名和密碼進行保護, 嘗試與 建立連接會話(String host,int port,String user,String pass,boolean usesAuth,boolean secure)構造函數通過提供host,port,user,pass和usersAuth爲true,安全爲false, 但它無法與用戶名和密碼建立連接併發出警告身份驗證錯誤:無法應對以下任何挑戰:{}。 我使用了正確的用戶名和密碼。如何使用couchdb4j api連接到couchdb數據庫用戶名和密碼?
3
A
回答
1
嘗試改爲積極維護的CouchDB數據庫http://www.ektorp.org/。
1
我通過編寫一個簡單的Curl類來解決相同的問題,該類簡化了通信並以String形式返回JSON,因爲這是返回的內容。
它應該是自我解釋,你可以閱讀提示的主要方法。
package path.to.the.class;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.codec.binary.Base64;
import Log;
/**
* This class takes on the simple task of doing http calls to any http web service like a web page or alike. Since the
* class is streamlined for JSON use this is the most easiest to set up.
*
* @author [email protected]
*
*/
public class Curl {
private Map<String, String> requestProperties = null;
private String charsetName = "UTF8";
public static final String GET = "GET";
public static final String PUT = "PUT";
public static final String POST = "POST";
public static final String HEAD = "HEAD";
public static final String DELETE = "DELETE";
/**
* This is the default constructor for Curl which takes it for granted that you want to communicate and read JSON.
* Most of the times this approach works even if plain html or text is requested.
*/
public Curl() {
requestProperties = new HashMap<String, String>();
requestProperties.put("Content-Type", "application/json");
}
/**
* With this alternate constructor a map containing header strings can be provided, useful if something apart from
* JSON is to be consumed.
*
* @param requestProperties
* a Map containing the header strings.
*/
public Curl(Map<String, String> requestProperties) {
this.requestProperties = requestProperties;
}
/**
* Public setter to enable setting charsetName.
*
* @param charsetName
* @return this instance to enable on liners.
*/
public Curl setCharsetName(String charsetName) {
this.charsetName = charsetName;
return this;
}
/**
* In the world of the web this is the command that a web browser does for you after you have entered an url into
* the address field. When using GET there should be no side effects on the site the data was requested from; the
* get method only consumes data and sends nothing.
*
* @param urlAsString
* the url to fetch from the internet.
* @return The response from the server
*/
public String get(String urlAsString) {
return doHttpCall(urlAsString, GET, null);
}
/**
* Put should be used when a resource should be sent to a server.
*
* @param urlAsString
* the url to the resource.
* @param doc
* the content to put
* @return The response from the server.
*/
public String put(String urlAsString, String doc) {
return doHttpCall(urlAsString, "PUT", doc);
}
/**
* Post should be used when a resource should be posted to a server.
*
* @param urlAsString
* the url to the resource.
* @param doc
* the content to put
* @return The response from the server.
*/
public String post(String urlAsString, String doc) {
return doHttpCall(urlAsString, "POST", doc);
}
/**
* Mostly to be considered as a get without the contents, Here implemented as an is the resource available function.
*
* @param urlAsString
* the url to the resource.
* @return The responseMessage from the server.
*/
public String head(String urlAsString) {
return doHttpCall(urlAsString, "HEAD", null);
}
/**
* Deletes a resource from an url. Be careful!
*
* @param urlAsString
* The url to the resource to delete.
* @return The response from the server.
*/
public String delete(String urlAsString) {
try {
return doHttpCall(urlAsString, "DELETE", null);
} catch (Exception e) {
Log.warn("No object to delete found at " + urlAsString + ".");
return "No object to delete found at " + urlAsString + ".";
}
}
/**
* This method does the actual HTTP communication to simplify the methods above.
*
* @param urlAsString
* The url to resource in question.
* @param method
* The method to be used.
* @param doc
* The resource to send or null if none.
* @return The response from the server.
*/
private String doHttpCall(String urlAsString, String method, String doc) {
StringBuffer result = new StringBuffer();
HttpURLConnection httpUrlConnection = null;
try {
URL url = new URL(urlAsString);
httpUrlConnection = (HttpURLConnection) url.openConnection();
httpUrlConnection.setDoInput(true);
httpUrlConnection.setRequestMethod(method);
if (url.getUserInfo() != null) {
String basicAuth = "Basic " + new String(new Base64().encode(url.getUserInfo().getBytes()));
httpUrlConnection.setRequestProperty("Authorization", basicAuth);
}
httpUrlConnection.setRequestProperty("Content-Length", "0");
for (String key : requestProperties.keySet()) {
httpUrlConnection.setRequestProperty(key, requestProperties.get(key));
}
if (doc != null && !doc.isEmpty()) {
httpUrlConnection.setDoOutput(true);
httpUrlConnection.setRequestProperty("Content-Length", "" + doc.getBytes(charsetName));
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(httpUrlConnection.getOutputStream(),
charsetName);
outputStreamWriter.write(doc);
outputStreamWriter.close();
}
readInputStream(result, httpUrlConnection.getInputStream());
} catch (RuntimeException e) {
Log.info(e.getMessage());
} catch (MalformedURLException e) {
Log.warn("The url '" + urlAsString + "' is malformed.");
} catch (IOException e) {
try {
result.append(e.getMessage());
readInputStream(result, httpUrlConnection.getErrorStream());
if ("".equals(result.toString())) {
result.append("Error ");
result.append(httpUrlConnection.getResponseCode());
result.append(" : ");
result.append(httpUrlConnection.getResponseMessage());
result.append(". Exception message is: [");
result.append(e.getMessage());
result.append("]");
}
} catch (IOException e1) {
}
} finally {
if ("HEAD".equalsIgnoreCase(method)) {
try {
result.append(httpUrlConnection.getResponseMessage());
} catch (IOException e) {
Log.fatal("This is as low as we can get, nothing worked!");
e.printStackTrace();
}
}
if (httpUrlConnection != null)
httpUrlConnection.disconnect();
}
return result.toString();
}
/**
* Local helper method that reads data from an inputstream.
*
* @param result
* The read text.
* @param inputStream
* The stream to read.
* @throws UnsupportedEncodingException
* @throws IOException
*/
private void readInputStream(StringBuffer result, InputStream inputStream) throws UnsupportedEncodingException,
IOException {
if (inputStream == null)
throw new IOException("No working inputStream.");
InputStreamReader streamReader = new InputStreamReader(inputStream, charsetName);
BufferedReader bufferedReader = new BufferedReader(streamReader);
String row;
while ((row = bufferedReader.readLine()) != null) {
result.append(row);
result.append("\n");
}
bufferedReader.close();
streamReader.close();
}
/**
* A main method to provide the possibility to use this exact class from the command line.
* <p>
* usage:
* <code>java -cp target/classes/. path.to.the.class.Curl http://server.domain.nu:port/path/to/resource method [data]</code>
* </p>
*
* @param args
* in order: url method data
*/
public static void main(String[] args) {
if (args.length < 2) {
System.out.println("usage: Curl path method [data]");
System.exit(0);
}
String url = args[0];
String method = args[1];
String data = args.length == 3 ? args[2] : null;
Curl curl = new Curl();
if (method.equals("head")) {
System.out.println(curl.head(url));
System.exit(0);
}
if (method.equals("put")) {
System.out.println(curl.put(url, data));
System.exit(0);
}
System.out.println(curl.doHttpCall(url, method, data));
}
}
0
嘗試改變配置參數required_valid_user真
相關問題
- 1. Java couchdb4j數據庫連接
- 2. 使用用戶名和密碼將用戶連接到數據庫
- 3. 在CouchDB _user數據庫中更改用戶名和密碼
- 4. 如何使用用戶名和密碼連接到mscrm?
- 5. 如何使用默認的Joomla數據庫用戶名和密碼連接到另一個數據庫?
- 6. 如何在CouchDB中創建一個數據庫,指示用戶名和密碼
- 7. Java數據庫用戶名和密碼
- 8. 如何將用戶名和密碼添加到Firebase數據庫
- 9. Laravel與用戶輸入用戶名和密碼連接數據庫
- 10. 如何連接使用的用戶名和密碼
- 11. 如何使用unicode用戶名,密碼,數據庫使用MySQLdb?
- 12. 如何使用密碼連接到sqlite數據庫
- 13. 如何使用哈希密碼連接到MySQL數據庫
- 14. 爲了連接到數據庫,應該提供哪個用戶名和密碼?
- 15. 檢查數據庫的用戶名或密碼OledDb連接
- 16. 如何連接到mongodb whern我有ip,用戶名和密碼
- 17. 如何連接到需要用戶名和密碼的網站
- 18. 如何將用戶名和密碼添加到Rethinkdb連接?
- 19. 連接到需要用戶名,密碼和使用Axis2/C
- 20. 使用用戶名和密碼連接到SQL Server
- 21. 使用用戶名和密碼連接到網絡驅動器
- 22. 通過使用用戶名和密碼連接到webservice
- 23. 用於登錄用戶名和密碼驗證的數據庫連接混淆
- 24. 如何使用Couchdb4j庫向CouchDB添加附件
- 25. 如何使用IP,用戶名和密碼連接到服務器?
- 26. 如何爲數據庫設置用戶名和密碼?
- 27. 如何找出mysql數據庫的密碼和用戶名?
- 28. 如何找出mysql數據庫的用戶名和密碼
- 29. 使用數據庫進行Java用戶名和密碼驗證
- 30. Sqlite3 - 使用用戶名和密碼創建數據庫
您能夠直接訪問數據庫,使用curl例如?有什麼結果像捲曲http://: @localhost:5984/_session? –
使用curl進行測試很複雜,因爲某些密碼字符對shell無效,而其他密碼字符無效捲曲。由於捲曲很容易,我同意首先嚐試。如果失敗,請嘗試使用Futon從瀏覽器登錄。至少這將確認基礎知識,名稱查找,聯網和身份驗證。 – JasonSmith
@Colin Ross,是的,我能夠從curl訪問它,這裏是對http://: @localhost:5984/_session **的迴應{「ok」:true,「userCtx」:{「name」 :「admin」,「roles」:[「_ admin」]},「info」:{「authentication_db」:「_ users」,「authentication_handlers」:[「」],「authenticated」:「」}} ** –
Subbarao