1.设置参数,使用协议读取https public static void paramSettings(HttpClient httpclient){ try { //Secure Protocol implementation. SSLContext ctx = SSLContext.getInstance("SSL"); //Implementation of a trust manager for X509 certificates X509TrustManager tm = new X509TrustManager() { public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException { } public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException { } public X509Certificate[] getAcceptedIssuers() { return null; } }; X509HostnameVerifier hostnameVerifier = new X509HostnameVerifier(){ public boolean verify(String hostname, SSLSession session) { // TODO Auto-generated method stub return true; } public void verify(String arg0, SSLSocket arg1) throws IOException { // TODO Auto-generated method stub } public void verify(String arg0, X509Certificate arg1) throws SSLException { // TODO Auto-generated method stub } public void verify(String arg0, String[] arg1, String[] arg2) throws SSLException { // TODO Auto-generated method stub } }; ctx.init(null, new TrustManager[] { tm }, null); SSLSocketFactory ssf = new SSLSocketFactory(ctx,hostnameVerifier); ClientConnectionManager ccm = httpclient.getConnectionManager(); //register https protocol in httpclient's scheme registry SchemeRegistry sr = ccm.getSchemeRegistry(); sr.register(new Scheme("https", 443, ssf)); sr.register(new Scheme("http", 80,PlainSocketFactory .getSocketFactory())); //set Time out httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, TIME_OUT); httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, TIME_OUT); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (KeyManagementException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
2.doGet
public static String getHTTPSJsonRequest(String url, String userName, String password) { logger.info("HttpsUtil : getHTTPSResult start............"); String result = null; try { HttpClient httpclient = getHttpClient(); HttpGet httpget = new HttpGet(url); String authString = userName + ":" + password; String authStringEnc = new String(Base64.encode(authString .getBytes())); httpget.addHeader("Authorization", "Basic " + authStringEnc); httpget.addHeader("content-type", "application/json"); ResponseHandler responseHandler = new BasicResponseHandler(); result = httpclient.execute(httpget, responseHandler); logger.debug("HttpsUtil : getHTTPSResult;result = " + result); // Create a response handler httpget.releaseConnection(); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } logger.info("HttpsUtil : getHTTPSResult end........"); return result; }
3.doPut
public static String putHTTPSJsonRequest(String url) { logger.info("HttpsUtil : putHTTPSResult start............"); logger.debug("url : " + url); String info = null; HttpClient httpclient = getHttpClient(); HttpPut httpPut = new HttpPut(url); try { HttpResponse httpresponse = httpclient.execute(httpPut); HttpEntity entity = httpresponse.getEntity(); info = EntityUtils.toString(entity, "UTF-8"); System.out.println("info = " + info); httpPut.addHeader("content-type", "application/json"); } catch (Exception e) { logger.debug("putData Exception url:{}", url, e); } finally { httpPut.releaseConnection(); } logger.info("HttpsUtil : putHTTPSResult end........"); // return info; return info; }
4.doPost
public static String postHTTPSJsonRequest(String url, Map<String, String> params) { logger.info("HttpsUtil : postHTTPSResult start............"); logger.debug("url : " + url); String response = null; String info = null; HttpClient httpclient = getHttpClient(); HttpPost httpPost = new HttpPost(url); try { List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); if (params != null && params.size() > 0) { Iterator keysIterator = params.keySet().iterator(); while (keysIterator.hasNext()) { String key = (String) keysIterator.next(); String value = params.get(key); nameValuePairs.add(new BasicNameValuePair(key, value)); } } if (nameValuePairs != null && nameValuePairs.size() > 0) { httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8")); } HttpResponse httpresponse = httpclient.execute(httpPost); HttpEntity entity = httpresponse.getEntity(); info = EntityUtils.toString(entity, "UTF-8"); System.out.println("info = " + info); httpPost.addHeader("content-type", "application/json"); } catch (Exception e) { logger.debug("putData Exception url:{}", url, e); } finally { httpPost.releaseConnection(); } logger.info("HttpsUtil : postHTTPSResult end........"); return info; }
public static HttpClient getHttpClientWithSSL(String SSLPath) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException, KeyManagementException, UnrecoverableKeyException{ HttpClient httpclient = new DefaultHttpClient(); KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); FileInputStream instream = new FileInputStream(new File(SSLPath)); try { trustStore.load(instream, "123456".toCharArray()); } finally { instream.close(); } SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore,"123456",trustStore); Scheme sch = new Scheme("https", socketFactory, 443); httpclient.getConnectionManager().getSchemeRegistry().register(sch); // httpclient.getConnectionManager().shutdown(); return httpclient; }
相关推荐
`doGet`方法通常用来处理读取或检索数据的请求,而`doPost`则用于处理数据提交或更新操作。这两个方法都需要接收`HttpServletRequest`和`HttpServletResponse`作为参数,以便从请求中获取数据并返回响应结果。 ### ...
- HttpServlet 类实现了 Servlet 接口,并提供了一些方法来处理不同的 HTTP 请求类型,如 doGet、doPost、doPut、doDelete、doHead、doOptions 和 doTrace。 - 最常用的是 doGet 和 doPost 方法,分别用于处理 GET...
这个类提供了`service()`方法的默认实现,该方法会基于请求的类型调用`doGet()`、`doPost()`等方法。开发者可以继承GenericServlet来创建自己的Servlet,但通常只在需要自定义`init()`和`destroy()`方法时这样做,...
- **`doPost()` 方法**:当用户提交表单时,如果表单的`method`属性设置为`POST`,那么服务器端将调用`doPost()`方法。此方法主要用于处理用户提交的数据。 - **`doPut()` 和 `doDelete()` 方法**:这两种方法分别...
- **接收请求**:Servlet监听HTTP请求,当ExtJS发起POST、PUT、DELETE或GET请求时,对应的Servlet方法(如doPost、doPut、doDelete、doGet)会被触发。 - **处理数据**:Servlet解析请求参数,根据请求类型执行...
在Servlet中,我们同样在`doPost`或`doPut`方法中处理这些请求,更新数据库中的记录: ```java protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, ...
- `service()`: 对每个请求,Servlet容器都会调用此方法,根据请求类型选择对应的方法(如doGet或doPost)。 - `destroy()`: 在Servlet实例被销毁之前调用,用于释放Servlet占用的资源。 3. **HTTP特定方法**: ...
2. **初始化**:Servlet容器调用init()方法对Servlet进行初始化,通常在这个阶段完成配置信息的读取等初始化工作。 3. **服务**:接下来Servlet容器将调用service()方法来处理客户端请求。在service()方法中,根据...
根据请求类型,Servlet容器会自动调用`doGet()`, `doPost()`, `doPut()`, 等具体处理方法。`service()`方法是线程安全的,意味着每个请求都会得到一个新的Servlet实例,除非Servlet实现了`SingleThreadModel`接口。 ...
通常通过`service()`方法实现,其中`doGet()`和`doPost()`是最常用的两个方法。 - **销毁**:当Web应用关闭或重新启动时,服务器调用`destroy()`方法释放Servlet所占用的资源。 #### Servlet编程接口 - **...
- `void init(ServletConfig config) throws ServletException`:初始化方法,与接口中的相同。 - `void destroy()`:销毁方法,释放资源。 - `String getServletInfo()`:返回关于Servlet的信息。 - `void log...
HttpServlet提供了一组默认的方法,如`doGet()`, `doPost()`, `doPut()`, `doDelete()`等,分别对应HTTP的GET、POST、PUT和DELETE等请求方法。 4. **ServletConfig接口**:每个Servlet实例都有一个ServletConfig...
- **`void doPost(HttpServletRequest request, HttpServletResponse response)`**:处理POST请求的方法。 此外,`HttpServlet`还支持其他HTTP方法,如`doHead()`、`doPut()`、`doDelete()`等,它们分别对应于HTTP...
通过这个方法,Servlet 可以进行必要的初始化工作,例如读取配置文件、创建数据库连接等。如果该方法抛出 `UnavailableException`,则该 Servlet 将不会进入服务状态。 - **`ServletConfig getServletConfig()`**:...
- **支持Servlet函数:** 除了doGET外,还可以实现doPOST、doPUT等函数来支持更多HTTP方法。 **2.3 开发第一个C++APP** - **cspApp.cpp文件:** 创建一个名为cspApp的C++文件,并实现相应的服务逻辑。 - **添加...
Servlet引擎载入Servlet后,Servlet引擎必须对Servlet进行初始化,在这一过程中,你可以读取一些固定存储的数据、初始化JDBC的连接以及建立与其他资源的连接。 在初始化过程中,javax.servlet.Servlet接口的init()...