POSTing sur HTTPS en Java, comportement étrange

J’ai décidé de poster des choses avec Java, mais je me suis retrouvé coincé dans une tâche aussi sortingviale. Ensuite, j’ai été frustré et j’ai tout réécrit en php, et cela fonctionne parfaitement. J’ai essayé de comparer les résultats des résultats Java et PHP, et ce que je remarque, c’est que Curl définit Content-Type: multipart / form-data; alors que Java (correctement?) le définit sur application / x-www-form-urlencoded; mais cela déclenche une erreur de 500 serveur interne . Que diable se passe-t-il?

Débogage Java:

2017/12/11 19:10:36:236 EET [DEBUG] wire - http-outgoing-0 >> "POST /auth/register HTTP/1.1[\r][\n]" 2017/12/11 19:10:36:243 EET [DEBUG] wire - http-outgoing-0 >> "Cookie: XSRF-TOKEN=eyJpdiI6InJJRjdORWdzUVcwQVZMR2l2WnNHUWc9PSIsInZhbHVlIjoiZVFSc0V5MCtURFE4dTZHZGlwVXdna0VGMGZtNzh0aEF4eXJcL0ZcL3d6QWM4NVdJejJaeEptUDFcL0ZDeXBEOGlDMTltbEQ4cFg0c1wvK3h4Nkp3VEhJTmFRPT0iLCJtYWMiOiIzNmRjZGNmZGNmYTFkYTQzODQ2NjFkZWY3ZWVlZGJmNzBiNDFhNTQwNDU3ODAzMTA4MGNhYWRiY2VhNDU2ZmU2In0%3D;laravel_session=eyJpdiI6ImF3cGg2TUFvWm54b3J4Nml5NnlBYlE9PSIsInZhbHVlIjoiXC85ZzdJS2drRWxlWGExXC93bHFVNXRtTmFtTmcyblJ4cXY4eUhCY2toaWJGaFBcL2NjQllKekVrUWFvblhydWtSeHpySm4yWGlWbHE3Y3dXZjFxd3lXV3c9PSIsIm1hYyI6IjRlNGZiMjU4NDRmMWVjZjc1YzExYWM4ZjJlMTUyNzI0ZTY3NTAwYTUyZTdlNTdiZmQ2ZDg1NTk1OGE4OGQ3ZGMifQ%3D%3D[\r][\n]" 2017/12/11 19:10:36:243 EET [DEBUG] wire - http-outgoing-0 >> "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0[\r][\n]" 2017/12/11 19:10:36:243 EET [DEBUG] wire - http-outgoing-0 >> "Accept: */*[\r][\n]" 2017/12/11 19:10:36:243 EET [DEBUG] wire - http-outgoing-0 >> "Content-Length: 146[\r][\n]" 2017/12/11 19:10:36:243 EET [DEBUG] wire - http-outgoing-0 >> "Content-Type: application/x-www-form-urlencoded; charset=UTF-8[\r][\n]" 2017/12/11 19:10:36:243 EET [DEBUG] wire - http-outgoing-0 >> "Host: xxx[\r][\n]" 2017/12/11 19:10:36:243 EET [DEBUG] wire - http-outgoing-0 >> "Connection: Keep-Alive[\r][\n]" 2017/12/11 19:10:36:243 EET [DEBUG] wire - http-outgoing-0 >> "Accept-Encoding: gzip,deflate[\r][\n]" 2017/12/11 19:10:36:243 EET [DEBUG] wire - http-outgoing-0 >> "[\r][\n]" 2017/12/11 19:10:36:244 EET [DEBUG] wire - http-outgoing-0 >> "username=jnfjkwejf&_token=ZTE8t5XDf2vmBcAWpSenELkEtqvhIp9FBSy0E1Ez&domain=xxx&password=qweqweqwe&password_confirmation=qweqweqwe&captcha=U8Aq3" 2017/12/11 19:10:36:319 EET [DEBUG] wire - http-outgoing-0 << "HTTP/1.1 500 Internal Server Error[\r][\n]" 

Code Java:

 HttpHost target = new HttpHost("xxx", 443, "https"); SSLContext sslcontext = SSLContexts.createSystemDefault(); SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory( sslcontext, new Ssortingng[] { "TLSv1.2", "SSLv3" }, null, SSLConnectionSocketFactory.getDefaultHostnameVerifier()); Registry socketFactoryRegistry = RegistryBuilder.create() .register("http", PlainConnectionSocketFactory.INSTANCE) .register("https", sslConnectionSocketFactory) .build(); PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(socketFactoryRegistry); CloseableHttpClient httpsclient = HttpClients.custom() .setSSLSocketFactory(sslConnectionSocketFactory) .setConnectionManager(cm) .build(); ArrayList formparams = new ArrayList(); formparams.add(new BasicNameValuePair("username", "jnfjkwejf")); formparams.add(new BasicNameValuePair("_token", _token)); formparams.add(new BasicNameValuePair("domain", "xxx")); formparams.add(new BasicNameValuePair("password", "qweqweqwe")); formparams.add(new BasicNameValuePair("password_confirmation", "qweqweqwe")); formparams.add(new BasicNameValuePair("captcha", captcha)); UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, Consts.UTF_8); HttpPost httppost = new HttpPost("/auth/register"); String fullCookie = captchaConn.getHeaderFields().get("Set-Cookie").get(1).split(";")[0] + ";" + captchaConn.getHeaderFields().get("Set-Cookie").get(0).split(";")[0]; httppost.addHeader("Cookie", fullCookie); httppost.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0"); httppost.addHeader("Accept", "*/*"); httppost.setEntity(entity); CloseableHttpResponse execute = httpsclient.execute(httppost); 

PHP:

 > POST /auth/register HTTP/1.1 Host: xxx User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0 Accept: */* Cookie: XSRF-TOKEN=eyJpdiI6Im4zSXNHanBpM1FmXC9oZ0dEeFZFbElRPT0iLCJ2YWx1ZSI6Inh2S2FKSktQMGFxREMwVU5KZHkzSVEzaXFyVnNWdEpiQ1IzVFd4Y3c3RWFxQXV6YXFRaFNuQlBSU2M5bEs5azh2dG9zSjFoQWlNVW00dGgzeW1IeFhRPT0iLCJtYWMiOiI4MTUwZTk3ZDNkZDMyOTkxMjRkNjRhY2I5MjEzMDZmNTk5NzUwYjA5NDY3YmY0OWQ4YzQ1NmMxNTVjZDIwNzNkIn0%3D; laravel_session=eyJpdiI6IldnbGd1STlUSVZXc0NFbWZLTVhGZEE9PSIsInZhbHVlIjoiRDBndzlSZFJLUlZHYWdJWmppZmNCOWRYV2liNnV1NFJkMzNoNEpyVGRBaXlEUm94MzNSbmZ4YVdDeHM0OTNNa21qZmQ1Tjd4UVJrK2pYS3BUQUhsbFE9PSIsIm1hYyI6ImZkNTBmZjYxOTc0ZmUxNzIzZTNlOTBkYWJmMzBkODhkODkxNTk1Mjc5Nzg5MTI5NmJkYzJlYzBjOTEyMGM0OTUifQ%3D%3D Content-Length: 718 Expect: 100-continue Content-Type: multipart/form-data; boundary=------------------------e939a893b45ae97c < HTTP/1.1 100 Continue < HTTP/1.1 302 Found < Server: nginx/1.10.3 < Content-Type: text/html; charset=UTF-8 < Transfer-Encoding: chunked < Connection: keep-alive < Cache-Control: no-cache ... 

Options de curl:

 curl_setopt($ch, CURLOPT_URL, 'https://xxx/auth/register'); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0'); curl_setopt($ch, CURLOPT_COOKIEFILE, __DIR__ . '.\cookiejar'); curl_setopt($ch, CURLOPT_COOKIEJAR, __DIR__ . '.\cookiejar'); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields); 

Si votre serveur attend Content-Type: multipart/form-data , vous devez utiliser MultipartEntityBuilder au lieu de UrlEncodedFormEntity .

Votre code de création d’entité pourrait être quelque chose comme

 HttpEntity entity = MultipartEntityBuilder .create() .addTextBody("username", "jnfjkwejf") .addTextBody("_token", _token) .addTextBody("domain", "xxx") .addTextBody("password", "qweqweqwe") .addTextBody("password_confirmation", "qweqweqwe") .addTextBody("captcha", captcha) .build(); 

Reportez-vous à ce lien pour plus de détails. Apache HttpClient faisant un post de formulaire multipart

Mise à jour – Un sharepoint plus.

Votre en-tête de cookie ne semble pas être correct dans le cas de Java. Les paires de cookies doivent être séparées par un point-virgule et un espace ( '; ' ). il manque de la place dans votre cas java. Réf. – https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cookie

curl peut utiliser l’un des deux encodages, tout comme php ou java, il s’agit simplement de configurer le type de requête correctement.

urlencoded is old style multipart est le nouveau venu dans le bloc, Java doit être capable de générer les deux, peut-être en trouvant un nouvel exemple Java?

Aussi, pourquoi ne pas essayer de créer vous-même la demande réelle et d’envoyer les octets réels que vous devez envoyer, en créant la demande vous-même octet par octet? considérez cela comme un exercice d’apprentissage.

Afin de déboguer pourquoi le serveur envoie 500 erreurs, je vous recommande de lire le code source de votre serveur et de consulter également les journaux de votre serveur. Il se peut que ce ne soit qu’une nouvelle façon de faire les choses.

btw j’aime l’ancienne façon, j’aime la simple. C’était simple, une demande POST, à mon époque… maintenant, la nouvelle façon de faire me semble inutile, trop d’octets, trop peu de données.