Hi, I need your help. Should I translate this code from c # to FOXPRO. Can you help me please
//Creo la request per creare un nuovo marker
string url = "HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
//Imposto il content-type
webRequest.ContentType = "text/html";
//Come metodo specifico POST, che in http indica una insert
webRequest.Method = "POST”;
string requestBody = " {"name": "my marker name", "brandId": 372, "cntId": 9725, "width":1.0, "publishedFrom":"Fri, 2 Sep 2016 GMT", "publishedFrom":"Fri, 9 Sep 2016 GMT", "imageStreamName":"myImageFileName" } ";
//Creo una stringa contenente la data e l’ora odierna in formato RFC1123, utilizzando il timezone UTC
string date = DateTime.UtcNow.ToString("R");
//Imposto l’header “Date” della richiesta
webRequest.Headers.Add("Date ", date);
//Imposto l’header “Authorization” della richiesta
webRequest.Headers.Add("Authorization", publicKey + ":" + CreateSignature(webRequest.Method, requestBody, privateKey, date, “/cms/markers”));
//Se la richiesta non ha come metodo GET, probabilmente ho un requestBody da inviare, quindi imposto lo stream della richiesta con il contenuto (requestBody) che voglio inviare
if (webRequest.Method != "GET")
{
StreamWriter streamWriter = new StreamWriter(webRequest.GetRequestStream());
streamWriter.Write(requestBody);
streamWriter.Flush();
streamWriter.Close();
}
//Invio la richiesta al server
string result = "";
HttpWebResponse httpResponse = (HttpWebResponse)webRequest.GetResponse();
//Leggo il contenuto della risposta del server
using (StreamReader streamReader = new StreamReader(httpResponse.GetResponseStream()))
result = streamReader.ReadToEnd();
//Se la risposta ha codice diverso da 200 (HttpStatusCode.OK in C#) si tratta di un errore http
if (httpResponse.StatusCode != HttpStatusCode.OK)
throw new Exception(result);
//A questo punto nella stringa result ho il contenuto della risposta, es:
{"id":8472, "name":"my marker name", "brandId":958, "cntId":1256, "cntType":1, "parentCntId":0, "crsId":"2_405", "width":1.0, "imageUrl":" "publishedFrom":"2016-09-02T00:00:00Z", "publishedTo":"2016-09-09T00:00:00Z" }
public string CreateSignature(string requestMethod, string requestBody, string privateKey, string requestDate, string requestPath)
{
string contentMD5 = GetMd5Hash(requestBody);
string stringToSign = GetStringToSign(requestMethod, contentMD5, requestDate, requestPath);
byte[] sha1Bytes = Encoding.UTF8.GetBytes(stringToSign);
byte[] key = Encoding.UTF8.GetBytes(privateKey);
byte[] hashedKey = SHA1.Create().ComputeHash(key);
HMACSHA1 hmac = new HMACSHA1(hashedKey);
byte[] hashedBytes = hmac.ComputeHash(sha1Bytes);
return System.Convert.ToBase64String(hashedBytes);
}
public GetStringToSign(string requestMethod, string contentMD5, string requestDate, string requestPath)
{
return string.Format("{0}|{1}|{2}|{3}", requestMethod, contentMD5, requestDate, requestPath);
}
//Creo la request per creare un nuovo marker
string url = "HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
//Imposto il content-type
webRequest.ContentType = "text/html";
//Come metodo specifico POST, che in http indica una insert
webRequest.Method = "POST”;
string requestBody = " {"name": "my marker name", "brandId": 372, "cntId": 9725, "width":1.0, "publishedFrom":"Fri, 2 Sep 2016 GMT", "publishedFrom":"Fri, 9 Sep 2016 GMT", "imageStreamName":"myImageFileName" } ";
//Creo una stringa contenente la data e l’ora odierna in formato RFC1123, utilizzando il timezone UTC
string date = DateTime.UtcNow.ToString("R");
//Imposto l’header “Date” della richiesta
webRequest.Headers.Add("Date ", date);
//Imposto l’header “Authorization” della richiesta
webRequest.Headers.Add("Authorization", publicKey + ":" + CreateSignature(webRequest.Method, requestBody, privateKey, date, “/cms/markers”));
//Se la richiesta non ha come metodo GET, probabilmente ho un requestBody da inviare, quindi imposto lo stream della richiesta con il contenuto (requestBody) che voglio inviare
if (webRequest.Method != "GET")
{
StreamWriter streamWriter = new StreamWriter(webRequest.GetRequestStream());
streamWriter.Write(requestBody);
streamWriter.Flush();
streamWriter.Close();
}
//Invio la richiesta al server
string result = "";
HttpWebResponse httpResponse = (HttpWebResponse)webRequest.GetResponse();
//Leggo il contenuto della risposta del server
using (StreamReader streamReader = new StreamReader(httpResponse.GetResponseStream()))
result = streamReader.ReadToEnd();
//Se la risposta ha codice diverso da 200 (HttpStatusCode.OK in C#) si tratta di un errore http
if (httpResponse.StatusCode != HttpStatusCode.OK)
throw new Exception(result);
//A questo punto nella stringa result ho il contenuto della risposta, es:
{"id":8472, "name":"my marker name", "brandId":958, "cntId":1256, "cntType":1, "parentCntId":0, "crsId":"2_405", "width":1.0, "imageUrl":" "publishedFrom":"2016-09-02T00:00:00Z", "publishedTo":"2016-09-09T00:00:00Z" }
public string CreateSignature(string requestMethod, string requestBody, string privateKey, string requestDate, string requestPath)
{
string contentMD5 = GetMd5Hash(requestBody);
string stringToSign = GetStringToSign(requestMethod, contentMD5, requestDate, requestPath);
byte[] sha1Bytes = Encoding.UTF8.GetBytes(stringToSign);
byte[] key = Encoding.UTF8.GetBytes(privateKey);
byte[] hashedKey = SHA1.Create().ComputeHash(key);
HMACSHA1 hmac = new HMACSHA1(hashedKey);
byte[] hashedBytes = hmac.ComputeHash(sha1Bytes);
return System.Convert.ToBase64String(hashedBytes);
}
public GetStringToSign(string requestMethod, string contentMD5, string requestDate, string requestPath)
{
return string.Format("{0}|{1}|{2}|{3}", requestMethod, contentMD5, requestDate, requestPath);
}