[英]How can I diagnose the difference between a PHP curl POST and a C# WebClient POST?
我有如下的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);
像這樣的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;
}
當我在IIS 7.5上執行PHP代碼時,得到了成功的響應。 當我執行看似相同的C#代碼時,由於我的IP地址而被阻止。 我使用“ accesskey”值應該是為了防止IP地址檢查,它使用PHP代碼,而不是C#代碼。
我可以使用哪些工具檢查離開請求HTTP? 在每種情況下發布的表單數據都是相同的,包括密碼哈希,所以我想我正在尋找消息中的標頭或編碼差異。
根據您粘貼的功能,過帳數據不相同。 不是由一個長鏡頭。
您在C#代碼中具有不同的accesskey和許多其他參數,如果再次使用相同的參數運行它將發生什么?
我不是ac#用戶,但Aggregate
函數將在結尾處返回帶有“&”的字符串,這可能會導致問題。
您可以檢查urlencode
函數的返回值php和c#是否使用相同的rfc編碼
如果不是這種情況,則可以使用wireshark分析數據包。
我想我使用的是Fiddler,而不是Wireshark,我不記得了,但是區別在於cURL隱式地添加了WebClient
沒有的兩個標頭。 以下新增功能解決了該問題:
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.