开发者

Is there a COM accessible library to allow URL Encoding?

Using VB6. It's not hard to roll your own, but I wond开发者_开发百科ered if was a prebuilt one out there?


Prompted by Bob's comment: Google found this wrapper for UrlEscape in a newsgroup post from Karl Peterson.

Private Declare Function UrlEscape Lib "Shlwapi.dll" Alias "UrlEscapeA" ( _
  ByVal pszURL As String, ByVal pszEscaped As String, ByRef pcchEscaped As Long, _
  ByVal dwFlags As Long) As Long

Private Const URL_DONT_ESCAPE_EXTRA_INFO As Long = &H2000000

Private Function EscapeURL(ByVal URL As String) As String
' Purpose:  A thin wrapper for the URLEscape API function. '
Dim EscTxt As String
Dim nLen As Long

' Create a maximum sized buffer. '
nLen = Len(URL) * 3
EscTxt = Space$(nLen)

If UrlEscape(URL, EscTxt, nLen, URL_DONT_ESCAPE_EXTRA_INFO) = 0 Then
  EscapeURL = Left$(EscTxt, nLen)
End If
End Function

Disclaimer: I haven't tried this code myself.


You should use CoInternetParseUrl(), with URL_ENCODE.

The sample from MSDN, modified for your purposes. Of course, you'll have to figure out how to call CoInternetParseUrl() from VB6, but you seem well on your way to that.

#include <wininet.h>

// ...

WCHAR encoded_url[INTERNET_MAX_URL_LENGTH];
DWORD encoded_url_len = ARRAYSIZE(encoded_url);

// Assumes |url| contains the value you want to encode.

HRESULT hr = CoInternetParseUrl(url, PARSE_CANONICALIZE, URL_ENCODE, encoded_url, 
                        INTERNET_MAX_URL_LENGTH, & encoded_url_len, 0);
if (SUCCEEDED(hr)) {
  // Do stuff...
}

You may want to use PARSE_ENCODE instead of PARSE_CANONICALIZE, depending on your needs.

Also, consider using google-url. May be difficult since it's C++ and not COM based.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜