Les Snippets

Connexion

URLEncode

Niveau requis pour utiliser/comprendre cette source : 1 ( Débutant )
Créé le 04/03/2007 22:58:03 et initié par Cphil51 [Liste]
Date de mise à jour : 20/12/2007 11:05:16
Vue : 14037
Catégorie(s) : Réseau & Internet, Trucs & Astuces, Chaîne de caractères, Email & Messagerie, Web
Langages dispo pour ce code :
- C
- Windev
- PHP 4, PHP 5
- VB 2005, VB.NET 1.x
- ASP 3, ASP.NET 1.x, ASP.NET 2.x, VB 2005, VB.NET 1.x
- Java
- VB6, VBA



Langage : C
Date ajout : 04/03/2007
Posté par Cphil51 [Liste]
DateMAJ : 05/03/2007
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char * url_encode(const char * str)
{
  char * s = str;
  char * t = NULL;
  char * ret;
  char * validChars = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz:/.?=_-$(){}~&";
  char * isValidChar;
  int lenght = 0;
  // calcul de la taille de la chaine urlEncodée
  do{
    isValidChar = strchr(validChars, *s); // caractère valide?
    if(!isValidChar)
        lenght+=3; // %xx : 3 caractères
    else
        lenght++;  // sinon un seul
  }while(*++s); // avance d'un cran dans la chaine. Si on est pas à la fin, on continue...
  s = str;
  t = (char *)malloc(sizeof(char) * (lenght + 1)); // Allocation à la bonne taille
  if(!t) exit(EXIT_FAILURE);
  ret = t;
  //encodage
  do{
    isValidChar = strchr(validChars, *s);
    if(!isValidChar)
     sprintf(t, "%%%2X", *s), t+=3;







    else
        sprintf(t, "%c", *s), t++;
  }while(*++s);
  *t = 0; // 0 final
  return ret;
}
int main()
{
    char * urlEncoded = url_encode("while(*str++)sqfqsfq  ///\\ ");
    printf("%s", urlEncoded);
    free(urlEncoded); // Pensez à liberer l'espace alloué par l'encodeur!!
}
Langage : Windev
Date ajout : 14/03/2007
Posté par fabienlaps [Liste]
// Encode une URl avec uniquement un sous-ensemble de caractère ASCII
// Encode une URL
URLEncodée =  URLEncode("http://mon site/ma page")

Langage : PHP 4 , PHP 5
Date ajout : 01/04/2007
Posté par dom_ponge [Liste]
$urlencode urlencode(http://mon site/ma page);

Langage : VB.NET 1.x , VB 2005
Date ajout : 09/04/2007
Posté par Nix [Liste]

System.Web.HttpUtility.UrlEncode("Le texte à url encoder")

Remarque :
Il faut ajouter la référence à System.Web dans votre projet Winform.
Langage : VB.NET 1.x , VB 2005 , ASP 3 , ASP.NET 1.x , ASP.NET 2.x
Date ajout : 09/04/2007
Posté par Nix [Liste]
DateMAJ : 09/04/2007

Server.UrlEncode("Le texte à url encoder")

Langage : Java
Date ajout : 13/04/2007
Posté par Twinuts [Liste]
DateMAJ : 13/04/2007
String result = null;
......
try {
     //encodage
    result = java.net.URLEncoder.encode(
            "Le texte à url encoder",
            "UTF-8" /*Encodage des caractères*/
        );
} catch (java.io.UnsupportedEncodingException e) {
    e.printStackTrace();
}
......
try {
     //décodage
     result = java.net.URLDecoder.decode(
         "Le texte à url décoder",
         "UTF-8" /*Encodage des caractères*/
    );
} catch (java.io.UnsupportedEncodingException e) {
    e.printStackTrace( );
}


Langage : VB6 , VBA
Date ajout : 20/12/2007
Posté par Renfield [Liste]
DateMAJ : 20/12/2007

Private Const CP_UTF8 = 65001
Private Declare Function WideCharToMultiByte Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long, ByVal lpMultiByteStr As String, ByVal cchMultiByte As Long, ByVal lpDefaultChar As Long, ByVal lpUsedDefaultChar As Long) As Long

Public Function UTF8_Encode(ByRef vsInput As String) As String
Dim nLength As Long
   nLength = Len(vsInput)
   If nLength Then
       nLength = WideCharToMultiByte(CP_UTF8, 0, StrPtr(vsInput), nLength, 0, 0, 0, 0)
       UTF8_Encode = Space$(nLength)
       nLength = WideCharToMultiByte(CP_UTF8, 0, StrPtr(vsInput), -1, UTF8_Encode, nLength, 0, 0)
   End If
End Function 
Function UrlEncode(ByRef vsInput As String) As String Dim i As Long Dim xsAnsi() As Byte Dim nChar As Byte    xsAnsi = StrConv(UTF8_Encode(vsInput), vbFromUnicode)    For i = 0 To UBound(xsAnsi)        nChar = xsAnsi(i)        Select Case nChar            Case 48 To 57, 65 To 90, 97 To 122                UrlEncode = UrlEncode & Chr$(nChar)            Case Else                If nChar < 16 Then                    UrlEncode = UrlEncode & "%0" & Hex$(nChar)                Else                    UrlEncode = UrlEncode & "%" & Hex$(nChar)                End If        End Select    Next i End Function

Snippets en rapport avec : Url, Urlencode, Encode



Codes sources en rapport avec : Url, Urlencode, Encode

{Visual Basic, VB6, VB.NET, VB 2005} ENCODER UNE URL EN UTF8 DEPUIS WORD
Si on veut créer des URL correctes dans Word, il est nécessaire d'encoder convenablement tout ce qui...

{Visual Basic, VB6, VB.NET, VB 2005} URL2DOWN ACTIVEX _ COMPOSANT DÉDIÉ AU TELECHARGEMENT HTTP
URL2DOWN ACTIVEX _ COMPOSANT DÉDIÉ AU TELECHARGEMENT HTTP AVEC OU SANS PROXY ;) ---------------...

{Javascript / DHTML} ANALYSER & RÉCUPÉRER LES VARIABLES DE L'URL
Comme d'autres l'ont déjà fait, il faut analyser la chaine de l'URL pour les mettre dans un tableau ...

{C# / C#.NET} RÉCUPÉRER L'URL EN COURS DANS VOS NAVIGATEURS (INTERNET EXPLORER, FIREFOX, OPERA)
Cette source permet de récupérer l'URL de la page affichée dans les navigateurs ouverts. L'URL en co...

{Python} MODULE DE CRYPTOGRAPHIE
Ceci et un module qui facilite l'encodage & le decodage de type cesar & viginaire . Installation: ...

{JAVA / J2EE} REDIRECTION D'URL ET TRANSMISSION DE COOKIES
Ce programme donne des exemples de gestion des redirections d'url ainsi que de transmission de cooki...

{JAVA / J2EE} TELECHARGER UN FICHIER A PARTIR D'UNE URL
Cette classe permet de récupéré un fichier sur un site internet ou sur un serveur interne. ca peut ê...

{Javascript / DHTML} PASSATION DE VALEURS ENTRE FENÊTRES HTML
Le code de steveurcle http://www.javascriptfr.com/codes/MODAL-PAGE-APPELANTE_34989.aspx m'a donnée l...

{Python} TELECHARGEUR DE VIDEO (YOUTUBE)
Bonsoir a tous ceci est mon second code python il permet de telecharger une video sur youtube il...

{PHP} HTTP_BUILD_QUERY() POUR PHP 4
Version PHP 4 de la fonction http_build_query() disponible sous PHP 5 et ma foi bien pratique... .. ...