C language implementation of URL encode and URL decode

Source: Internet
Author: User
Tags url decode

Problems encountered in the project and Java communication, after reading the SessionKey, once again sent to the other side, always appear the session error prompt. The following information is then consulted:Java.net.URLEncode Coding and UrlDecode decoding problems

When a form in a Web page is submitted using the Post method, the type of data content is application/x-www-form-urlencoded, which is:
1. The characters "a"-"Z", "a"-"Z", "0"-"9", ".", "-", "*", and "_" are not encoded;
3. Convert non-textual content into "%xy" form, XY is a two-bit 16 binary value;
4. Place the & symbol between each name=value pair.
2. Convert the space to a plus sign (+);


Look at the following example: Open a Web page to fetch his sessionkey and then look at the values of the SessionKey used by other connection requests to compare:
  Reply When you log in to the website:   "SessionKey": "8ikbgdraqqzbasq5mrq+uxp6sba="
When you use the session to access other content on the site: sessionkey=8ikbgdraqqzbasq5mrq%2buxp6sba%3d
#include <stdlib.h> #include <string.h> #include <ctype.h> #include <sys/types.h> #include " Url.h "static unsigned char hexchars[] =" 0123456789ABCDEF ", Static int php_htoi (char *s) {int Value;int c;c = ((Unsigned cha R *) s) [0];if (Isupper (c)) C = ToLower (c); value = (c >= ' 0 ' && C <= ' 9 '? C-' 0 ': C-' a ' +) * 16;c = ((u nsigned char *) s) [1];if (Isupper (c)) C = ToLower (c); value + = c >= ' 0 ' && C <= ' 9 '? C-' 0 ': C-' a ' + 10;return (value);} Char *php_url_encode (char const *s, int len, int *new_length) {Register unsigned char c;unsigned char *to, *start;unsigned Char Const *from, *end;from = (unsigned char *) S;end = (unsigned char *) s + len;start = to = (unsigned char *) calloc (1, 3*len+1) while (from < end) {c = *from++;if (c = = ") {*to++ = ' + ';} else if ((C < ' 0 ' && c! = '-' &&am P c! = '. ') || (C < ' A ' && C > ' 9 ') | | (C > ' Z ' && C < ' a ' && c! = ' _ ') | | (C > ' z ')) {to[0] = '% '; to[1] = Hexchars[c >> 4];to[2] = hexchars[c & 15];to + + 3;} else {*to++ = c;}} *to = 0;if (new_length) {*new_length = To-start;} Return (char *) start; int Php_url_decode (char *str, int len) {char *dest = Str;char *data = Str;while (len--) {if (*data = ' + ') {*dest = ';} else if (*data = = '% ' && len >= 2 && isxdigit ((int) * (data + 1)) && isxdigit ((int) * (data + 2)) {*dest = (char) php_htoi (data + 1);d ata + = 2;len-= 2;} else {*dest = *data;} data++;d est++;} *dest = '/0 '; return dest-str;}

Reference: http://blog.csdn.net/langeldep/article/details/6264058

C language implementation of URL encode and URL decode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.