我正在尝试向需要基本身份验证的URL发送POST请求,我已经在这里找到了解决方案,但我的servlet仍然没有得到结果,这是我的代码,我希望你可以帮助我解决这个问题,因为我正处在一场真正的斗争中。
//Basic Auth + envoi réponse
//backUrl=http://....../ws
String webPage = backUrl ;
String name = "CLT_NAGT";
String password = "azerty@1234567";
String authString = name + ":" + password;
System.out.println("auth string: " + authString);
byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
String authStringEnc = new String(authEncBytes);
System.out.println("Base64 encoded auth string: " + authStringEnc);
//Connexion
URL url = new URL(webPage);
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.setDoOutput(true);
urlConnection.setRequestProperty("Authorization", "Basic " + authStringEnc);
urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.connect();
//Envoi du JSON
OutputStreamWriter outputWriter = new OutputStreamWriter(urlConnection.getOutputStream());
outputWriter.write(Json);//Json is a json.toString() object
outputWriter.flush();
outputWriter.close();
在我的浏览器中我得到一个空白页面,我的eclipse不会抛出异常或任何错误。我该如何解决这个问题?
答案 0 :(得分:0)
这是我的servlet(服务器端)
public class ServletAO extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("application/json");
//récupération du resultat de l'exécution de l'AO
PA865AccessObject ao = (PA865AccessObject) req.getAttribute("ao");
String aoResp = ao.getMessage();
String rep="";
DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Date today = Calendar.getInstance().getTime();
String reportDate = df.format(today);
//récupération des paramètres de la requete
// String nomPrenom = req.getParameter("nomPrenom");
String adrLigne1 = req.getParameter("adresse1");
String adrLigne2 = req.getParameter("adresse2");
String adrLigne3 = req.getParameter("adresse3");
String adrLigne4 = req.getParameter("adrLigne4");
String adrLigne5 = req.getParameter("adrLigne5");
String codePostal = req.getParameter("codepostal");
String ville = req.getParameter("ville");
String pays = req.getParameter("pays");
String numContrat = req.getParameter("numContrat");
String numAbonne = req.getParameter("numAbonne");
String idRequest = req.getParameter("idRequest");
String backUrl = req.getParameter("backurl");
if(aoResp.indexOf("MISE A JOUR EFFECTUEE")>-1)
{
rep="2-Taken";
}
else
{
rep="4-Dropped";
}
//Fichier JSON
JSONObject technical= new JSONObject();
try {
technical.put("topic", "fr-sicl-Address_Contact_Change_Request-v1");
technical.put("organization", "fr");
technical.put("sa","SICLIENT");
technical.put("nature", "business");
technical.put("sk", "0");
technical.put("version", "1");
technical.put("source_event_id", "ID");
technical.put("event_id", "IDCalcule");
technical.put("event_date_time", reportDate);
technical.put("emission_date_time", reportDate);
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println(technical.toString());
JSONObject payload= new JSONObject();
try {
payload.put("distrib_customer_ids", numAbonne);
payload.put("party_customer_id", "A verifier");
payload.put("contract_id",numContrat);
payload.put("correlation_id", idRequest);
payload.put("request_id", idRequest);
payload.put("request_issued_date_time", reportDate);
payload.put("request_event_type", rep);
payload.put("request_event_reason", "PA865");
payload.put("source_system", "CIARD");
payload.put("party_row_id", "A verifier");
payload.put("replaced_address_date_time", reportDate);
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println(payload.toString());
JSONObject newAdress = new JSONObject();
try {
newAdress.put("recipient_line1", adrLigne1);
newAdress.put("line2", adrLigne2);
newAdress.put("line3",adrLigne3);
newAdress.put("street_line4", adrLigne4);
newAdress.put("line5", adrLigne5);
newAdress.put("zip_locality_line6", "");
newAdress.put("country_label_line7", "");
newAdress.put("zip_postal_code", codePostal);
newAdress.put("locality", ville);
newAdress.put("country", pays);
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println(newAdress.toString());
String Json =
technical.toString()+payload.toString()+newAdress.toString();
//Basic Auth + envoi réponse
String webPage = backUrl ;
String name = "CLT_NAGT";
String password = "azerty@1234567";
String authString = name + ":" + password;
System.out.println("auth string: " + authString);
byte[] authEncBytes = Base64.encodeBase64(authString.getBytes());
String authStringEnc = new String(authEncBytes);
System.out.println("Base64 encoded auth string: " + authStringEnc);
//Connexion
URL url = new URL(webPage);
HttpURLConnection urlConnection =
(HttpURLConnection)url.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.setDoOutput(true);
urlConnection.setRequestProperty("Authorization", "Basic " +
authStringEnc);
urlConnection.setRequestProperty("Content-Type",
"application/json");
urlConnection.connect();
//Envoi du JSON
OutputStreamWriter outputWriter = new
OutputStreamWriter(urlConnection.getOutputStream());
outputWriter.write("");
outputWriter.flush();
outputWriter.close();
//resp.flushBuffer();
}
}