Java 具有基本身份验证的Servlet POST请求
我正在尝试向需要基本身份验证的URL发送POST请求,我到处寻找解决方案,但我的servlet仍然没有得到结果,这是我的代码,我希望您能帮助我解决这个问题,因为我正处于真正的挣扎中Java 具有基本身份验证的Servlet POST请求,java,servlets,basic-authentication,Java,Servlets,Basic Authentication,我正在尝试向需要基本身份验证的URL发送POST请求,我到处寻找解决方案,但我的servlet仍然没有得到结果,这是我的代码,我希望您能帮助我解决这个问题,因为我正处于真正的挣扎中 //Basic Auth + envoi réponse //backUrl=http://....../ws String webPage = backUrl ; String name = "CLT_NAGT"; String pa
//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不会抛出异常或任何错误。如何修复此问题?这是我的servlet(服务器端)
您在哪里阅读回复?谢谢您的回复。您正在谈论inputstreamreader吗?如果您在浏览器中没有得到任何响应,那么您对客户端代码的期望是什么?您需要共享服务器端实现,特别是您尝试调用的servlet代码。我只需要将json.toString()发送到指定的URL。这是我的servlet:此部分属于答案,不适用于部分帖子。此外,我们不能查看您的servlet代码。您需要告诉我们您是否从servlet得到任何响应。您需要调试此部分,检查服务器日志,尝试任何现有的客户端(如postman),并验证服务器端实现
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();
}
}