使用ASP.NET的Bitrix24 Rest API发布请求

时间:2017-02-27 09:32:42

标签: c# php asp.net rest bitrix

Bitrix24 CRMwebhook functional向CRM添加潜在客户(客户)。所有文档都是用php编写的,但我想使用ASP.NET。以下是他们在php上的表现:

$queryUrl  = 'https://restapi.bitrix24.ru/rest/1/31uhq2q855fk1foj/crm.lead.add.json';
$queryData = http_build_query(array(
    'fields' => array(
        "TITLE" => "NEW LEAD"
    ),
    'params' => array(
        "REGISTER_SONET_EVENT" => "Y"
    )
));

$curl = curl_init();
curl_setopt_array($curl, array(
    CURLOPT_SSL_VERIFYPEER => 0,
    CURLOPT_POST => 1,
    CURLOPT_HEADER => 0,
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => $queryUrl,
    CURLOPT_POSTFIELDS => $queryData
));

$result = curl_exec($curl);
curl_close($curl);

$result = json_decode($result, 1);

我正在尝试使用ASP.NET执行相同的操作,但将错误400作为响应。我几乎可以肯定问题出在请求参数行const string data = @"[{""fields"":{""title"":""Test""}}]";中。我已经尝试了很多组合,但没有任何效果。

const string url = @ "https://companyname.bitrix24.ru/rest/14/31uhq2q855fk1foj/crm.lead.add.json";
const string data = @"[{""fields"":{""title"":""Test""}}]";

HttpClient client = new HttpClient();
client.BaseAddress = new Uri(url);
byte[] cred = Encoding.UTF8.GetBytes("email:password");
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(cred));
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

HttpContent content = new StringContent(data, Encoding.UTF8, "application/json");
HttpResponseMessage messge = client.PostAsync(url, content).Result;
string description;
if (messge.IsSuccessStatusCode) {
    string result = messge.Content.ReadAsStringAsync().Result;
    description = result;
}

3 个答案:

答案 0 :(得分:5)

Bitrix24支持非常快乐"用PHP和不知道其他语言:(经过一些调查,我发现匿名对象和json.net的方式。你的样本应该是这样的:

var data = new {
  fields = new {
    TITLE = "NEW LEAD"
  },
  @params = new {
    REGISTER_SONET_EVENT = "Y"
  }
};
var contentText = JsonConvert.SerializeObject(data);

var content = new StringContent(contentText, Encoding.UTF8, "application/json");
// and so on with HttpClient

12月13日更新:

有时您无法(或者不想)将字段名称直接放入匿名对象中。因此,可以使用字典:

var data = new
{
    ID = someId,
    FIELDS = new Dictionary<string, object>()
    {
        [options.SomeFieldName] = fieldValue,
    },
};

答案 1 :(得分:0)

我用以下代码解决了问题:

public void CreateLead( string title, decimal opportunity, string contactName, string phoneNumber, string email )
    {
        try
        {                
            string accessToken = GetNewAccessToken();

            string url = string.Format( "https://{0}/rest/crm.lead.add.json", portal_name );

            var data = new
            {
                fields = new
                {
                    TITLE = title,
                    CURRENCY_ID = "RUB",
                    STATUS_ID = "NEW",
                    OPENED = "Y",
                    OPPORTUNITY = opportunity,
                    ASSIGNED_BY_ID = 46,
                    COMPANY_TITLE = contactName,
                    PHONE =  new List<PHONE>() { new PHONE() { VALUE_TYPE = "WORK", TYPE_ID = "PHONE", VALUE = phoneNumber } }.ToArray(),
                    EMAIL = new List<EMAIL>() { new EMAIL() { VALUE_TYPE = "WORK", TYPE_ID = "EMAIL", VALUE = email } }.ToArray()
                },
                @params = new
                {
                    REGISTER_SONET_EVENT = "Y"
                }
            };

            BitrixLead lead = new BitrixLead();

            lead.TITLE = title;
            lead.CURRENCY_ID = "RUB";
            lead.STATUS_ID = "NEW";
            lead.OPENED = "Y";
            lead.OPPORTUNITY = opportunity.ToString();                

            if (!string.IsNullOrEmpty( contactName ))
                lead.COMPANY_TITLE = contactName;

            if (!string.IsNullOrEmpty( phoneNumber ))
                lead.PHONE = new List<PHONE>() { new PHONE() { VALUE_TYPE="WORK", TYPE_ID="PHONE", VALUE = phoneNumber }}.ToArray();

            if (!string.IsNullOrEmpty( email ))
                lead.EMAIL = new List<EMAIL>() { new EMAIL() { VALUE_TYPE = "WORK", TYPE_ID = "EMAIL", VALUE = email } }.ToArray();


            PostToAPI( url, accessToken, data );
        }
        catch (Exception exc)
        {
        }
    }

private void PostToAPI( string url, string token, object data )
    {
        string json = Newtonsoft.Json.JsonConvert.SerializeObject( data );

        var http = (HttpWebRequest)WebRequest.Create( new Uri( url ) );
        http.Accept = "application/json; charset=utf-8";
        http.ContentType = "application/json; charset=utf-8";
        http.Method = "POST";            
        http.Headers.Add( "Authorization", "Bearer " + token );

        UTF8Encoding encoding = new UTF8Encoding();
        Byte[] bytes = encoding.GetBytes( json );

        Stream newStream = http.GetRequestStream();
        newStream.Write( bytes, 0, bytes.Length );
        newStream.Close();

        var response = http.GetResponse();            
    }

答案 2 :(得分:-1)

Public Sub webServicesDatos(params As String, ws As String, idusuario As Integer, accion As String, catalogo As String,
                                Idcampo As String, registro As Integer, idregistro As Integer, pagina As String,
                                 cat As String, nomcampo As String, bd As String, campoUpdate As String, SistemasNom As String, IdTransaccion As Integer,
                                Proceso As Integer, Evento As Integer, Usuarioadd As String)
        qryLogBitrix = ""
        request = WebRequest.Create(ws & params)
        postData = params
        Dim data As Byte() = Encoding.UTF8.GetBytes(postData)

        request.Method = "POST"
        request.ContentType = "application/json"
        request.ContentLength = data.Length

        Dim stream As Stream = request.GetRequestStream()
        stream.Write(data, 0, data.Length)
        stream.Close()

        response = request.GetResponse()
        Using reader As New StreamReader(response.GetResponseStream())
            SR2 = reader.ReadToEnd()
        End Using

        Dim ValidaResult As Object = New JavaScriptSerializer().Deserialize(Of Object)(SR2)
End Sub


Dim consumir As ConsumirRestApi = New ConsumirRestApi
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim builder As StringBuilder = New StringBuilder()
    If nombre <> "" Or ap_paterno <> "" Or ap_materno <> "" Then
        parametros = HttpUtility.UrlEncode("FIELDS[TITLE]") + "=" + HttpUtility.UrlEncode(nombre) + " " + HttpUtility.UrlEncode(ap_paterno) + " " + HttpUtility.UrlEncode(ap_materno)
        builder.Append(parametros)
    End If
    If nombre <> "" Or nombre <> Nothing Then
        parametros = "&" + HttpUtility.UrlEncode("FIELDS[NAME]") + "=" + HttpUtility.UrlEncode(nombre)
        builder.Append(parametros)
    End If
    If idExpediente <> "0" Or idExpediente <> Nothing Or idExpediente <> "" Then
        parametros = "&" + HttpUtility.UrlEncode("FIELDS[UF_ID_EXPEDIENTE]") + "=" + HttpUtility.UrlEncode(idExpediente)
        builder.Append(parametros)                                     
    End If

    If accion = "INSERT" Then
        carpeta = "crm.lead.add.json?"                                  
    ElseIf accion = "UPDATE" Then
        carpeta = "crm.lead.update.json?"
    ElseIf accion = "DELETE" Then
        carpeta = "crm.lead.delete.json?"
    End If

    consumir.webServicesDatos(builder.ToString, "https://" & ip & "" & carpeta & "", idUsuario, accion, procesos, Id_formulario, id_bitrix, idCiclo, ruta, cat, nomcamp, bdinter, nomcampo, sistemanom, idCiclo, pro, evento, "")

End Sub