Переписать код PHP с curl на C#
22.08.2016, 20:25. Показов 2001. Ответов 0
Пытался перевести данную функцию с php на с#, но что-то не получилось:
PHP | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
| function request($endpoint, $post = null, $login = false)
{
if (!$this->isLoggedIn && !$login) {
throw new InstagramException("Not logged in\n");
return;
}
$headers = [
'Connection: close',
'Accept: */*',
'Content-type: application/x-www-form-urlencoded; charset=UTF-8',
'Cookie2: $Version=1',
'Accept-Language: en-US',
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, Constants::API_URL.$endpoint);
curl_setopt($ch, CURLOPT_USERAGENT, Constants::USER_AGENT);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_VERBOSE, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_COOKIEFILE, $this->IGDataPath."$this->username-cookies.dat");
curl_setopt($ch, CURLOPT_COOKIEJAR, $this->IGDataPath."$this->username-cookies.dat");
if ($post) {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
}
$resp = curl_exec($ch);
$header_len = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($resp, 0, $header_len);
$body = substr($resp, $header_len);
curl_close($ch);
if ($this->debug) {
echo "REQUEST: $endpoint\n";
if (!is_null($post)) {
if (!is_array($post)) {
echo 'DATA: '.urldecode($post)."\n";
}
}
echo "RESPONSE: $body\n\n";
}
return [$header, json_decode($body, true)];
} |
|
На c#:
C# | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
| public string request(string endpoint, Dictionary<string, string> post = null, bool login = false)
{
if(!this.isLoggedIn || login)
return "";
var client = new RestClient(Constants.API_URL);
client.CookieContainer = new CookieContainer();
client.UserAgent = Constants.USER_AGENT;
var loginRequest = new RestRequest(endpoint, Method.POST);
//loginRequest.AddHeader("Connection", "Close");
loginRequest.AddHeader("Accept", "*/*");
loginRequest.AddHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
//loginRequest.AddHeader("Cookie2", "");
loginRequest.AddHeader("Accept-Language", "en-US");
if (post != null)
foreach (KeyValuePair<string, string> pair in post)
loginRequest.AddParameter(pair.Key, pair.Value);
//curl_setopt($ch, CURLOPT_COOKIEFILE, $this->IGDataPath."$this->username-cookies.dat");? how load this from file
//curl_setopt($ch, CURLOPT_COOKIEJAR, $this->IGDataPath."$this->username-cookies.dat");?
var loginResponse = client.Execute(loginRequest);
return loginResponse.Content;
} |
|
Вызываю request вот так:
C# | 1
2
3
4
5
6
7
8
9
10
11
12
13
| public string userFriendship(string userId)
{
Dictionary<string, string> data = new Dictionary<string,string>();
data.Add("_uuid", this.uuid);
data.Add("_uid", this.username_id);
data.Add("user_id", userId);
data.Add("_csrftoken", this.token);
string hash = hash_hmac("sha256", JsonConvert.SerializeObject(data), Constants.IG_SIG_KEY);
data.Add("signed_body", hash);
data.Add("ig_sig_key_version", Constants.SIG_KEY_VERSION);
return this.request("/friendships/show/" + userId + "/", data);
} |
|
Ответ от сервера: { "status": "fail", "message": "login_required" }
Думаю что проблема в том, что у меня нет мозгов А ещё и в том что куки нужно впихнуть.
Пробовал client.CookieContainer.Add(new Cookie(name, value)) либо не помогло, либо не те куки вписывал.
После дня мучения решил обратиться сюда.
Добавлено через 19 минут
Тему можно удалять, разобрался.
Я просто лол.
0
|