I'm trying to identify the differences between the two strings during a URL get request (using LWP::Simple).
I have a URL, say http://www.example.com?param1=x&param2=y&param3=z
I make sure any blank inputs are also taken care of, but that is irrelevant at this point, because I am making sure all parameters are exactly the same.
Also, the hard-coded URL is copied and pasted from the generated URL.
This URL works when I do the following:

my $url = "http://www.example.com?param1=x&param2=y&param3=z";
my $content = get($url);

Yet, when I build the URL from parameters provided by a user, the get request does not work (Error: 500 from the site).
I have compared the two URLs by printing them out, and see zero differences. I've tried removing all of the potential invisible characters.

The output for the generated code and static string, assuming user input is the same as the static string (which is what I'm making sure to do):

http://www.example.com?param1=x&param2=y&param3=z
http://www.example.com?param1=x&param2=y&param3=z

I'm assuming printing the outputs removes characters I can't see. I've also followed a solution at http://www.perlmonks.org/?node_id=882590 and it is pointing out differences, but I don't know why, considering I see none at all.
Has anyone run into this problem before? Please let me know if I need to clarify anything or need to provide additional information.




EDIT: Problem and Solution
So, after using mob's suggestion to identify differences, I found there was a null character in the generated URL that was not getting printed in the output. That is:
http://www.example.com?param1=x&param2=y&param3=z was actually
http://www.example.com?param1=x&param2=y&param3=\000z.
I used a simple regex: $url =~ s/\000//g; to remove that (and any other) null value.

有帮助吗?

解决方案

Use a data serialization function to inspect your strings for hidden characters.

$url1 = "http://www.example.com?param1=x&param2=y";
$url2 = "http://www.example.com?param1=x&param2=y\0";
$url3 = "http://www.example.com?param1=x&param2=y\n";

use JSON;
print JSON->new->pretty(1)->encode( [$url1,$url2,$url3] );
# Result:
# [
#   "http://www.example.com?param1=x&param2=y",
#   "http://www.example.com?param1=x&param2=y\u0000",
#   "http://www.example.com?param1=x&param2=y\n"
# ]


use Data::Dumper;
$Data::Dumper::Useqq = 1;
print Dumper($url1,$url2,$url3);
# Result:
# $VAR1 = "http://www.example.com?param1=x&param2=y";
# $VAR2 = "http://www.example.com?param1=x&param2=y\0";
# $VAR3 = "http://www.example.com?param1=x&param2=y\n";

其他提示

Clearly the string you have built is different from the hard-coded one. If you write code like this

my $ss = 'http://www.example.com?param1=x&param2=y&param3=z';
print join(' ', map " $_", $ss =~ /./g), "\n";
print join(' ', map sprintf('%02X', ord), $ss =~ /./g), "\n";

then you will be able to see the hex value of each character in the string, and you can compare the two of them more accurately. For instance, the code above outputs

 h  t  t  p  :  /  /  w  w  w  .  e  x  a  m  p  l  e  .  c  o  m  ?  p  a  r  a  m  1  =  x  &  p  a  r  a  m  2  =  y  &  p  a  r  a  m  3  =  z
68 74 74 70 3A 2F 2F 77 77 77 2E 65 78 61 6D 70 6C 65 2E 63 6F 6D 3F 70 61 72 61 6D 31 3D 78 26 70 61 72 61 6D 32 3D 79 26 70 61 72 61 6D 33 3D 7A
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top