Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Http 如何在codenameone ConnectionRequest中进行基本身份验证_Http_Authentication_Codenameone - Fatal编程技术网

Http 如何在codenameone ConnectionRequest中进行基本身份验证

Http 如何在codenameone ConnectionRequest中进行基本身份验证,http,authentication,codenameone,Http,Authentication,Codenameone,有人问过这个问题,但我的问题仍然存在 final String info = "{\"tweet\":\"example\"}"; String url = "https://api.orchestrate.io/v0" + "/" + "roars/seinecle/events/?pretty=true"; ConnectionRequest request = new ConnectionRequest() { @Override

有人问过这个问题,但我的问题仍然存在

    final String info = "{\"tweet\":\"example\"}";

    String url = "https://api.orchestrate.io/v0" + "/" + "roars/seinecle/events/?pretty=true";
    ConnectionRequest request = new ConnectionRequest() {
        @Override
        protected void buildRequestBody(OutputStream os) throws IOException {
            os.write(info.getBytes("UTF-8"));
        }

    };

    request.setUrl(url);
    request.setPost(true);
    request.setHttpMethod("PUT");
    request.setContentType("application/json");
    request.addRequestHeader("Authorization", "Basic MzUzOThlOTQtZjUzMy00ZGZkLTgx==");
我得到一个401错误。助手控制台说我应该作为请求编写的内容:

这对我们有用:

String encoded = Base64.encodeNoNewline((user + ":" + pass).getBytes());
auth.addRequestHeader("Authorization", "Basic " + encoded);
看起来您正在进行Twitter连接,请在git存储库中查看我们以前的TwitterRESTService类及其源代码。不过有点过时了

另外,请查看我们如何编写UTF-8来解决此问题:

if(shouldWriteUTFAsGetBytes()) {
    os.write(val.toString().getBytes("UTF-8"));
} else {
    OutputStreamWriter w = new OutputStreamWriter(os, "UTF-8");
    w.write(val.toString());
}
Java实现之间存在一些细微的差异,这些差异可能只适用于其中一种,而不适用于另一种

您可能还需要在消息正文中的前后换行


我建议在模拟器工具中使用我们的网络监视器,并查看传出的请求。

认为我解决了问题,但pb仍然。。。实际上我不做推特,那只是一个占位符名称。我添加了一些其他想法。我认为401可能是一个误导性的错误信息。