小编Wei*_*uan的帖子

Java HttpURLConnection.getInputStream但获得401 IOException

我正在为Java中的CouchDB编写REST客户端.以下代码应该是非常标准的:

    this.httpCnt.connect();
    Map<String, String> responseHeaders = new HashMap<>();
    int i = 1;
    while (true){
        String headerKey = this.httpCnt.getHeaderFieldKey(i);
        if (headerKey == null)
            break;
        responseHeaders.put(headerKey, this.httpCnt.getHeaderField(i));
        i++;
    }
    InputStreamReader reader = new InputStreamReader(this.httpCnt.getInputStream());
    StringBuilder responseBuilder = new StringBuilder();
    char[] buffer = new char[1024];
    while(true){
        int noCharRead = reader.read(buffer);
        if (noCharRead == -1){
            reader.close();
            break;
        }
        responseBuilder.append(buffer, 0, noCharRead);
    }
Run Code Online (Sandbox Code Playgroud)

我想测试如果身份验证失败会发生什么.但是,如果身份验证失败,在调用getInputStreamHttpURLConnection时,我直接得到一个IOException,说服务器响应401.我想如果服务器响应某些东西,无论成功或失败,它应该能够读取服务器返回的任何内容.我确信在这种情况下服务器会返回正文中的一些文本,因为如果我GET使用curl对服务器执行操作并且身份验证失败,我会获得一个JSON对象作为响应正文,其中包含一些错误消息.

有没有办法仍然得到响应机构即使401?

java httpresponse httpurlconnection http-status-code-401

7
推荐指数
2
解决办法
1万
查看次数

使用HTTPS进行CouchDB cookie身份验证

我使用HTTPS完全按照文档中给出的指令配置了我的CouchDB(仅我将ssl端口更改为6161).我创建了两个管理员.然后,当我尝试使用cookie身份验证登录时,如下所示:

curl -kX POST https://localhost:6161/_session \
   -H 'Content-Type:application/x-www-form-urlencoded' \
   -d 'name=admin&password=admin'
Run Code Online (Sandbox Code Playgroud)

用户名和密码是正确的,因为我可以使用-ucurl选项登录.但我总是有unauthorized错误.我不确定这里有什么问题.

https curl couchdb

6
推荐指数
1
解决办法
1347
查看次数

单击WPF TreeView项目

我是WPF中的事件的问题.假设我有一个底层数据模型和一个树视图来呈现数据.我想做的最简单的事情是,当我点击一个项目时,我会对与该项目相关的基础数据做一些事情.

我尝试使用了MouseLeftButtonDown事件Textblock,但是发件人对象就是它Textblock本身,我无法访问底层数据.

现在我也尝试使用这样的MouseLeftButtonDown事件TreeViewItem:

<TreeView.ItemContainerStyle>
    <Style TargetType="{x:Type TreeViewItem}">
         <EventSetter Event="MouseLeftButtonDown" Handler="itemClicked"/>
    </Style>
</TreeView.ItemContainerStyle>
Run Code Online (Sandbox Code Playgroud)

但我没有得到处理程序调用.

那我该怎么做呢?有某种标准方法吗?

wpf treeview events click

5
推荐指数
1
解决办法
1万
查看次数