简体   繁体   English

如何诊断PHP curl POST和C#WebClient POST之间的区别?

[英]How can I diagnose the difference between a PHP curl POST and a C# WebClient POST?

I have PHP code such as the following: 我有如下的PHP代码:

$url = "http://my.parkpay.co.za/includes/api.php"; # URL to WHMCS API file goes here
$postfields["username"] = $username;
$postfields["password"] = md5($password);
$postfields["accesskey"] = "4a588e76-4e1c-4240-baad-4d2d6e3433cf";
$postfields["responsetype"] = "json";
$postfields["action"] = "addbillableitem";
$postfields["clientid"] = "3";
$postfields["description"] = "Test From PHP Code";
$postfields["amount"] = "250.00";
$postfields["invoiceaction"] = "nextinvoice";

foreach ($postfields AS $k=>$v) $query_string .= "$k=".urlencode($v)."&";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $query_string);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$jsondata = curl_exec($ch);
if (curl_error($ch)) {
    die("Connection Error: ".curl_errno($ch).' - '.curl_error($ch));
}
curl_close($ch);

And C# code like this: 像这样的C#代码:

public string PostTestBillableItem()
{
    const string username = "{username}";
    var passwordHash = Md5Hash({passwordHash});
    var formFields = new[]
                            {
                                new KeyValuePair<string, string>("username", username),
                                new KeyValuePair<string, string>("password", passwordHash),
                                new KeyValuePair<string, string>("accesskey", "4a588e76-4e1c-4240-baad-4d2d6e3433cf"),
                                new KeyValuePair<string, string>("responsetype", "json"),
                                new KeyValuePair<string, string>("action", "addbillableitem"),
                                new KeyValuePair<string, string>("clientid", "3"),
                                new KeyValuePair<string, string>("description", "Payment Towards Web Design Project"),
                                new KeyValuePair<string, string>("amount", "250.00"),
                                new KeyValuePair<string, string>("invoiceaction", "nextinvoice")
                            };
    var postData = formFields.Aggregate("", (current, pair) => current + (pair.Key + "=" + HttpUtility.UrlEncode(pair.Value) + "&"));
    var client = new WebClient();
    var result = client.UploadString("http://my.parkpay.co.za/includes/api.php", postData);
    return result;
}

When I execute the PHP code, on IIS 7.5, I get a successful response. 当我在IIS 7.5上执行PHP代码时,得到了成功的响应。 When I execute my seemingly identical C# code, I get blocked because of my IP address. 当我执行看似相同的C#代码时,由于我的IP地址而被阻止。 My use of the `accesskey" value is supposed to prevent the IP address check. It does with the PHP code, not with the C# code. 我使用“ accesskey”值应该是为了防止IP地址检查,它使用PHP代码,而不是C#代码。

What tools can I use to examine the leaving request HTTP? 我可以使用哪些工具检查离开请求HTTP? The form data posted in each case is identical, including the password hash, so I guess I'm looking for a header or encoding difference in the messages. 在每种情况下发布的表单数据都是相同的,包括密码哈希,所以我想我正在寻找消息中的标头或编码差异。

According to the functions you have pasted, post data is not identical. 根据您粘贴的功能,过帐数据不相同。 Not by a long shot. 不是由一个长镜头。

You have different accesskeys and a alot more parameters in the C# code, what happens if you run it again with the same parameters? 您在C#代码中具有不同的accesskey和许多其他参数,如果再次使用相同的参数运行它将发生什么?

  1. I'm not ac# user but the Aggregate function would return a string with "&" at the end which might cause a problem. 我不是ac#用户,但Aggregate函数将在结尾处返回带有“&”的字符串,这可能会导致问题。

  2. you can check the return value of urlencode function whether php and c# use the same rfc encoding 您可以检查urlencode函数的返回值php和c#是否使用相同的rfc编码

  3. if these are not the case then you can use wireshark to analyze the packet. 如果不是这种情况,则可以使用wireshark分析数据包。

I think I used Fiddler, not Wireshark, I can't recall, but the difference was that cURL implicitly added two headers that WebClient didn't. 我想我使用的是Fiddler,而不是Wireshark,我不记得了,但是区别在于cURL隐式地添加了WebClient没有的两个标头。 The following additions solved the problem: 以下新增功能解决了该问题:

var client = new WebClient();
client.Headers.Add("Accept", "/");
client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM