powered by simpleCommunicator - 2.0.59     © 2026 Programmizd 02
Целевая тема:
Создать новую тему:
Автор:
Закрыть
Цитировать
Форумы / ASP.NET [игнор отключен] [закрыт для гостей] / PHP to C# HttpWebResponse,WebRequest
1 сообщений из 1, страница 1 из 1
PHP to C# HttpWebResponse,WebRequest
    #39191200
Фотография yardie
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Участник
Привет всем!
Помогите пожалуйста перевести PHP в C#

Код: 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.
<?php	
   /* REST keys taken from REST APIs Reference tab in control interface of Epom Server */
   $rest_publickey  = '81535ee0e7678a5f';
   $rest_privatekey = '2b1460c932d85e16';
   $rest_timestamp  = time()*1000;

   /* Data of user that should be rigistered through API */
   $user_name      = 'TesTest_api223';
   $password       = '1234567';
   $email          = 'test_api@test.com';
   $aclRole        = '3';
   $phone          = '1231231234';
   $first_name     = 'TesTest1234';
   $last_name      = 'TesTest123';

   /* Hash for request authentication and validation */
   $hash = md5($user_name.$password.$email.$rest_privatekey.$rest_timestamp);

   /* REST API mount point for use registration */
    $rest_url = "https://platform.wingatemedia.com/rest-api/user-sign-up/$rest_publickey/$hash/$rest_timestamp.do";
    
   /* Data being posted */
    $post_data = array(
       'username'       => $user_name,
       'password'       => $password,
       'email'          => $email,
       'aclRole'        => $aclRole,
       'phone'          => $phone,
       'firstName'      => $first_name,
       'lastName'       => $last_name,
       'websiteUrl'     => $website_url       
   );
   
    /* Options of HTTP request; http key should be used when posting to https url */
   $options = array(
       'http' => array(
           'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
           'method'  => 'POST',
           'content' => http_build_query($post_data)
       )
   );
   
   /* Actual call to REST URL */
   $context  = stream_context_create($options);
   $result   = file_get_contents($rest_url, false, $context);

   echo "$result\n";
?>



Я сделал вот так, но сервер выдает ошибку Data fingerprint mismatch

Код: 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.
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.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
 public void SingIn(string _user_name, string _password, string _email, string _aclRole, string _phone, string _first_name, string _last_name, string _website_url)
        {
            string WebResponse;
             /* REST keys taken from REST APIs Reference tab in control interface of  Server */
            string rest_publickey = "01010101010101";
            string rest_privatekey = "1010101010101";

            DateTime UNIX_EPOCH = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
            long rest_timestamp = (long)((DateTime.UtcNow - UNIX_EPOCH).TotalSeconds) *1000;

            /* Data of user that should be rigistered through API */
            string user_name = _user_name;
            string password = _password;
            string email = _email;
            string aclRole = _aclRole;
            string phone = _phone;
            string first_name = _first_name;
            string last_name = _last_name;
            string website_url = _website_url;

            /* Hash for request authentication and validation */
            string sCrcBase = string.Format("{0}.{1}.{2}.{3}.{4}", user_name, password, email, rest_privatekey, rest_timestamp);
            System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] bSignature = md5.ComputeHash(Encoding.ASCII.GetBytes(sCrcBase));
            StringBuilder sbSignature = new StringBuilder();
            foreach (byte b in bSignature)
                sbSignature.AppendFormat("{0:x2}", b);
            string hash = sbSignature.ToString();

            /* REST API mount point for use registration */
            string rest_url = "https://site.com/rest-api/user-sign-up/" + rest_publickey + "/" + hash + "/" + rest_timestamp.ToString() + ".do";

            Dictionary<string, string> post_data = new Dictionary<string, string>();
            post_data.Add("username", user_name);
            post_data.Add("password", password);
            post_data.Add("email", email);
            post_data.Add("aclRole", aclRole);
            post_data.Add("phone", phone);
            post_data.Add("firstName", first_name);
            post_data.Add("lastName", last_name);
            post_data.Add("websiteUrl", website_url);

            NameValueCollection nvc = new NameValueCollection();
            foreach (var kvp in post_data)
            {
                nvc.Add(kvp.Key.ToString(), kvp.Value.ToString());
            }

            var post_data_to_send = ToQueryString(nvc);

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(rest_url);
            request.Method = "POST";
            request.ContentType = "Content-type: application/x-www-form-urlencoded\r\n";
            request.ContentLength = post_data_to_send.Length;
            var requestStream = request.GetRequestStream();

            requestStream.Write(post_data_to_send, 0, post_data_to_send.Length);
            requestStream.Close();

            HttpWebResponse resp = (HttpWebResponse)request.GetResponse();
           
            using (StreamReader stream = new StreamReader(resp.GetResponseStream(), Encoding.UTF8))
            {
                WebResponse = stream.ReadToEnd();
            }

        }

        private byte[] ToQueryString(NameValueCollection nvc)
        {
            var array = (from key in nvc.AllKeys
                         from value in nvc.GetValues(key)
                         select string.Format("{0}={1}", HttpUtility.UrlEncode(key), HttpUtility.UrlEncode(value)))
                .ToArray();
            return Encoding.Unicode.GetBytes("?" + string.Join("&", array));
        }
...
Рейтинг: 0 / 0
1 сообщений из 1, страница 1 из 1
Форумы / ASP.NET [игнор отключен] [закрыт для гостей] / PHP to C# HttpWebResponse,WebRequest
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]