我一直在摆弄我的程序,并且一直在使用允许 BASIC 身份验证的 urlread 的修改版本。问题是我必须将以下代码行包含到基本 urlread 函数中:
urlConnection.setRequestProperty('Authorization', 'Basic passphrase');
...其中密码是“user:pass”的 base64 编码字符串。如果我将密码直接放入该行的字符串中,程序将正常工作,但当我尝试连接以获得结果“基本密码”字符串时,麻烦就开始了。最初我只是:
['Basic', ' ', passphrase]
之后不起作用,我在命令窗口中进行了一些探索和实验。:
passphrase = 'somerandompassphrase';
teststr1 = ['Basic', ' ', passphrase];
teststr2 = ['Basic', ' ', 'somerandompassphrase'];
teststr3 = 'Basic somerandompassphrase';
strcmp(teststr1, teststr2)
strcmp(teststr1, teststr3)
strcmp(teststr2, teststr3)
输出为 1,或者每个都为 true(如预期)。但是,如果我采用“somerandompassphrase”的base64encode(即“c29tZXJhbmRvbXBhc3NwaHJhc2U=”):
encoded = base64encode(passphrase);
teststr1 = ['Basic', ' ', encoded];
teststr2 = ['Basic', ' ', 'c29tZXJhbmRvbXBhc3NwaHJhc2U='];
strcmp(teststr1, teststr2)
输出为0或错误。但这不应该是真的吗? base64encode 函数可以在此处找到。
即使经过快速测试:
strcmp(encoded, 'c29tZXJhbmRvbXBhc3NwaHJhc2U=')
输出仍然是 0。
请帮忙,我不知道发生了什么。
I've been fiddling around with my program and I've been using a modified version of urlread that allows for BASIC authentication. The problem is that I have to include the following line of code to the base urlread function:
urlConnection.setRequestProperty('Authorization', 'Basic passphrase');
...where passphrase is the a base64 encoded string of 'user:pass'. If I place the passphrase directly into the string on that line the program will work just fine, the trouble starts when I try to concatenate to get that resulting 'Basic passphrase' string. Initially I just had:
['Basic', ' ', passphrase]
After that did not work I did some exploring and experimenting around in the command window.:
passphrase = 'somerandompassphrase';
teststr1 = ['Basic', ' ', passphrase];
teststr2 = ['Basic', ' ', 'somerandompassphrase'];
teststr3 = 'Basic somerandompassphrase';
strcmp(teststr1, teststr2)
strcmp(teststr1, teststr3)
strcmp(teststr2, teststr3)
The output is 1, or true for each one (as expected). However if I take the base64encode of 'somerandompassphrase' (which is 'c29tZXJhbmRvbXBhc3NwaHJhc2U='):
encoded = base64encode(passphrase);
teststr1 = ['Basic', ' ', encoded];
teststr2 = ['Basic', ' ', 'c29tZXJhbmRvbXBhc3NwaHJhc2U='];
strcmp(teststr1, teststr2)
The output is 0, or false. Shouldn't it be true though? The base64encode function can be found here.
Even from a quick test of:
strcmp(encoded, 'c29tZXJhbmRvbXBhc3NwaHJhc2U=')
The output is still 0.
Please help, I have no idea what's going on.
发布评论
评论(2)
如此处所示,您还可以使用来自 base64 编码器编解码器/” rel="nofollow">Apache Commons Codec Java 库与 MATLAB 捆绑在一起,可在类路径中使用:
As shown here, you can also use the base64 encoder from the the Apache Commons Codec Java library which comes bundled with MATLAB and is available on the classpath:
实际上,我在发布问题之前就已经弄清楚了这一点,但我想我会继续并保留它,以防人们遇到与我相同的问题。
问题出在 base64encode 函数上。它会自动在字符串末尾添加换行符,导致 strcmp 函数返回 false。要解决此问题,您可以在 base64encode 函数中包含一个可选参数的参数,如果您放入空白字符串,则不会在其末尾添加换行符,从而使其正常工作。
I actually figured this out right before I posted the question, but I figured I'd go ahead and leave it up in case people run into the same problem as I did.
The problem is from the base64encode function. It automatically adds a newline character to the end of the string, causing the strcmp function to return false. To fix this you can include a parameter for the optional parameter to the base64encode function, if you put in a blank string it won't add a newline character to the end of it causing it to work.