我必须将客户信息发送到服务器并从中获取响应。我的流程是使用POST从JSP发送客户信息到Servlet。从Servlet,我将发送数据并从响应中获取状态。但是当我向服务器发送数据时,我得到了http错误500.我可以将数据发送到HTTP或HTTPS。如果我必须使用HTTPS协议来请求服务器,我该如何实现呢?
这是servlet代码。
String refNo = request.getParameter("ref");
String amount = request.getParameter("amt");
response.setContentType(CONTENT_TYPE);
URL url;
URLConnection urlConn;
DataOutputStream cgiInput;
// URL of target page script.
url = new URL("http://uat.telemoney.com.sg/easypay2/paymentpage.do");
urlConn = url.openConnection();
urlConn.setDoInput(true);
urlConn.setDoOutput(true);
urlConn.setUseCaches(false);
urlConn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
String content = "mid=" + URLEncoder.encode(MERCHANT_ID) + "&ref=" + URLEncoder.encode(refNo)+ "&cur=" + URLEncoder.encode("SGD");
content += "&statusurl=" + URLEncoder.encode("http://www.winsondbs.com:8080/TestWeb/Servlet/DBSController") + "&paytype=" + URLEncoder.encode("2");
content += "&ccnum=" + URLEncoder.encode("5111111111111115") + "&ccdate=" + URLEncoder.encode("1511") + "&cccvv=" + URLEncoder.encode("989");
content += "&amt=" + URLEncoder.encode(amount) + "&creditCardName=" + URLEncoder.encode("raymond") + "&cccvv=" + URLEncoder.encode("989");
System.out.println(content);
// Send POST output.
cgiInput = new DataOutputStream(urlConn.getOutputStream());
cgiInput.writeBytes(content);
cgiInput.flush();
cgiInput.close();
// reads the CGI response and print it inside the servlet content
BufferedReader cgiOutput =
new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
PrintWriter servletOutput = response.getWriter();
servletOutput.print("<html><body><h1>This is the Source Servlet</h1><p />");
String line = null;
while (null != (line = cgiOutput.readLine())){
servletOutput.println(line);
}
cgiOutput.close();
servletOutput.print("</body></html>");
servletOutput.close();
以下是错误:
Jan 16, 2013 8:33:04 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet DBSController threw exception
java.io.IOException: Server returned HTTP response code: 500 for URL: http://uat.telemoney.com.sg/easypay2/paymentpage.do
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at servlet.com.dbs.www.DBSController.doPost(DBSController.java:163)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)
我该如何解决?
答案 0 :(得分:0)
您不应该编写DataOutputStream。另一端的服务器是否期望DataOutputStream。
替换此
cgiInput = new DataOutputStream(urlConn.getOutputStream());
cgiInput.writeBytes(content);
与
cgiInput = urlConn.getOutputStream());
cgiInput.write(content.getBytes());