Je dois faire poster une demande à un service Web pour authentifier l'utilisateur avec nom d'utilisateur et mot de passe.
J'ai un problème avec la demande postale suivante: p>
private String makePost(String action, ConnectionParametrData [] parameters) throws IOException { StringBuilder urlBuild = new StringBuilder(); urlBuild.append(scheme).append("://www.").append(authority).append(action); URL url = new URL(urlBuild.toString()); URLConnection urlConnection = url.openConnection(); urlConnection.setDoInput(true); urlConnection.setDoOutput(true); urlConnection.setUseCaches(false); urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); DataOutputStream printout = new DataOutputStream(urlConnection.getOutputStream()); String content = getParameters(parameters); printout.writeBytes(content); printout.flush(); printout.close(); BufferedReader in = null; in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()), 8192); StringBuffer sb = new StringBuffer(""); String line = ""; String newLine = System.getProperty("line.separator"); while((line = in.readLine()) != null) { sb.append(line + newLine); } in.close(); return sb.toString(); }
3 Réponses :
Dans votre premier code de code, je ne vois pas où vous définissez des paramètres de poste pour la connexion et le mot de passe.
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair> { new BasicNameValuePair("login", "myusername"), new BasicNameValuePair("password", "somepassword")}; httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
@james noirs
pour le paramètre de réglage J'ai une méthode: p> @fildor
Exception est jetée du serveur. P> p>
public String getPostPage(String postUrl, NameValuePair[] data, String cookie) { String html = ""; PostMethod method = null; String contentStr = null; try { method = new PostMethod(postUrl); method.addRequestHeader("User-Agent", USER_AGENT); method.addRequestHeader("Host", "asqx.moni.gucheng.com"); method.addRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); method.addRequestHeader("Referer", "..."); method.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES); method.addRequestHeader("Cookie", cookie); method.addRequestHeader("X-MicrosoftAjax", "Delta=true"); method.addRequestHeader("Pragma", "no-cache"); // method.addRequestHeader("Accept-Encoding", "gzip, deflate"); method.addRequestHeader("Accept-Charset", "GB2312,utf-8;q=0.7,*;q=0.7"); method.addRequestHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); method.addRequestHeader("Accept-Language", "zh-cn,zh;q=0.5"); method.setRequestBody(data); int statusCode = client.executeMethod(method); if(statusCode == HttpStatus.SC_OK) { InputStream in = method.getResponseBodyAsStream(); if (in != null) { byte[] tmp = new byte[4096]; int bytesRead = 0; ByteArrayOutputStream buffer = new ByteArrayOutputStream(1024); while ((bytesRead = in.read(tmp)) != -1) { buffer.write(tmp, 0, bytesRead); } byte[] bt = buffer.toByteArray(); String gbk = new String(bt, "GBK"); String utf8 = new String(bt, "UTF-8"); if (gbk.length() < utf8.length()) { bt = null; bt = gbk.getBytes("UTF-8"); html = new String(bt, "UTF-8"); html = html.replaceFirst( "[cC][hH][aA][rR][sS][eE][tT]\\s*?=\\s*?([gG][bB]2312|[gG][bB][kK]|[gG][bB]18030)", "charset=utf-8"); } else if (gbk.length() > utf8.length()) { html = buffer.toString(); } else { html = buffer.toString(); } buffer.close(); contentStr = new String("abc".getBytes(), "UTF-8"); contentStr = html; in.close(); in = null; } } else { contentStr = null; } } catch (Exception e) { log.error(e); } finally { if (method != null) method.releaseConnection(); } return contentStr; } I use the mothod to post and get return content. Hope can help you.
"Erreur lancent" - sous forme d'exception? Avez-vous inspecté ce qui va réellement sur la ligne (Wireshark)?
@wilek pouvez-vous imprimer l'URI.Tostring () et le poster?