Почему URLEncoder Java не генерирует то, что делает urllib.parse.urlencode Python?JAVA

Программисты JAVA общаются здесь
Ответить Пред. темаСлед. тема
Anonymous
 Почему URLEncoder Java не генерирует то, что делает urllib.parse.urlencode Python?

Сообщение Anonymous »


My questions seem to be as same as this question: Java equivalent of Pythons urllib.urlencode(HashMap based UrlEncode) however, is not. Because its answer is not right (or I am doing something wrong that I am not aware of).

I have this Python3 code:

def create_vxttoken_data(path_uri, expiry, reuse, shared_secret): # Combine the parameters into a URL-encoded string message = [] message.extend([('pathURI', path_uri)]) message.extend([('expiry', expiry)]) message.extend([('reuse', reuse)]) print(message) url_encoded_message = urlencode(message) print(url_encoded_message) # ... rest of the code This is its output:

[('pathURI', 'http://my.domain.net/*'), ('expiry', 1553937508), ('reuse', 0)] pathURI=http%3A%2F%2Fmy.domain.net%2F%2A&expiry=1553937508&reuse=0 I tried to write it in Kotlin. So this is my code:

fun main(args: Array) { val queryParams = "pathURI=$PATH_URI&expiry=${expiryTime(10)}&reuse=$REUSE" println(queryParams) val encodedQueryParams = URLEncoder.encode(queryParams, Charsets.UTF_8.toString()) println(encodedQueryParams) // ... rest of the code This is its output:

pathURI=http://my.domain.net/*&expiry=1553938196&reuse=0 pathURI%3Dhttp%3A%2F%2Fmy.domain.net%2F*%26expiry%3D1553938196%26reuse%3D0 By compar of the second line of outputs you see that Kotlin/Java has converted = while Python didn't. Also, Kotlin/Java didn't translate * while Python does. I want Kotlin/Java generates output like what Python does. The rest of my code is creating a token based on this info. Consequently, my authentication fails.

Note: In my Kotlin/Java file, when I replace

val encodedQueryParams = URLEncoder.encode(queryParams, Charsets.UTF_8.toString()) By val encodedQueryParams = "pathURI=http%3A%2F%2Fmy.domain.net%2F%2A&expiry=1553937508&reuse=0" Then my rest code will generate a token which is exactly as same as what Python generates. Therefore, it indicates that something is wrong with my URLEncoder.encode.


Источник: https://stackoverflow.com/questions/554 ... encode-doe
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «JAVA»