Map<String, String> parseParams(String query) { ParameterParser pp = new ParameterParser(); List params = pp.parse(query, '&'); Map<String, String> result = new HashMap<String, String>(); for (Iterator it = params.iterator(); it.hasNext(); ) { NameValuePair pair = (NameValuePair) it.next(); result.put(pair.getName().toUpperCase(), pair.getValue()); } return result; };
/** * 将NameValuePairs数组转变为字符串 * * @param nameValues * @return */ protected String toString(NameValuePair[] nameValues) { if (nameValues == null || nameValues.length == 0) { return "null"; } StringBuffer buffer = new StringBuffer(); for (int i = 0; i < nameValues.length; i++) { NameValuePair nameValue = nameValues[i]; if (i == 0) { buffer.append(nameValue.getName() + "=" + nameValue.getValue()); } else { buffer.append("&" + nameValue.getName() + "=" + nameValue.getValue()); } } return buffer.toString(); }
private String getHttpRequestBody(PostMethod method) { StringBuilder strBody = new StringBuilder(); NameValuePair[] pairs = method.getParameters(); for (NameValuePair pair : pairs) { String name = pair.getName(); String value = pair.getValue(); strBody.append(name + "=" + value + ";"); } return strBody.toString(); }
/** * comparing the two name value pairs. ( header / param name value pairs) * * @return boolean - true if their contents are same. */ private boolean compareNameValuePairs(List<NameValuePair> pair1, List<NameValuePair> pair2) { if (pair1 != null && pair2 != null) { if (pair1 == null || pair2 == null) { return false; } if (pair1.size() != pair2.size()) { return false; } Iterator pair1_itr = pair1.iterator(); Iterator pair2_itr = pair2.iterator(); while (pair1_itr.hasNext()) { NameValuePair pair1_namevaluepair = (NameValuePair) pair1_itr.next(); NameValuePair pair2_namevaluepair = (NameValuePair) pair2_itr.next(); if (pair1_namevaluepair.getName().equals(pair2_namevaluepair.getName()) && pair1_namevaluepair.getValue().equals(pair2_namevaluepair.getValue())) { } else { pair1_itr = null; pair2_itr = null; return false; } } pair1_itr = null; pair2_itr = null; return true; } else { return true; } }
/** * parameter와 queryString 를 가져온다. * * @param method * @return */ private String getHttpInfoDumy(HttpMethod method) { NameValuePair[] params = null; String methodType = "GET"; String reqBody = null; if (method instanceof PostMethod) { params = ((PostMethod) method).getParameters(); methodType = "POST"; StringRequestEntity sre = (StringRequestEntity) ((PostMethod) method).getRequestEntity(); reqBody = sre.getContent(); } StringBuffer sb = new StringBuffer(); try { sb.append("#### getHttpInfoDumy ####"); sb.append("\n## " + methodType + " [" + method.getURI() + "], hscd[" + this.hashCode() + "]"); } catch (URIException e) { sb.append("\n## getParamsQueryStr- URIException " + e.getMessage() + "]"); return sb.toString(); } if (method.getQueryString() != null && method.getQueryString().length() > 0) sb.append("\n" + "## queryString[" + method.getQueryString() + "]"); if (params != null) { for (int i = 0; i < params.length; i++) { NameValuePair param = params[i]; sb.append( "\n" + "## POST body param[" + i + "], name[" + param.getName() + "], value[" + param.getValue() + "]"); } } if (reqBody != null) { sb.append("\n" + "## POST body String [" + reqBody + "]"); } sb.append("\n##########"); return sb.toString(); }
/** * setting the headers * * @param header -- the name value pair of the header to be set. */ private void setHeaders() { if (headers != null) { headers.clear(); } if (headerNameValuePair != null && headerNameValuePair.size() > 0) { Iterator<NameValuePair> mIterator = headerNameValuePair.iterator(); while (mIterator.hasNext()) { NameValuePair nameValuePair = mIterator.next(); headers.add(new Header(nameValuePair.getName(), nameValuePair.getValue())); nameValuePair = null; } mIterator = null; } }
public HttpMessageAdapter(Object message) { if (message instanceof Object[]) { // This case comes from the HttpMessageReceiver... Object[] messageParts = (Object[]) message; this.message = messageParts[0]; Map<Object, Object> headers = new HashMap<Object, Object>(); if (messageParts.length > 1) { Object second = messageParts[1]; if (second instanceof Map) { setupHeadersFromMap(headers, (Map) second); } else if (second instanceof Header[]) { setupHeadersFromHeaderArray(headers, (Header[]) second); } determineHttpVersion(headers); rewriteConnectionAndKeepAliveHeaders(headers); addInboundProperties(headers); } } else if (message instanceof HttpResponse) { this.message = message; return; } else { this.message = message; } String contentType = getStringProperty(HttpConstants.HEADER_CONTENT_TYPE, null); if (contentType != null) { // set the encoding Header contentTypeHeader = new Header(HttpConstants.HEADER_CONTENT_TYPE, contentType); HeaderElement values[] = contentTypeHeader.getElements(); if (values.length == 1) { NameValuePair param = values[0].getParameterByName("charset"); if (param != null) { setEncoding(param.getValue()); } } } }
/** * Parse RFC 2965 specific cookie attribute and update the corresponsing {@link * org.apache.commons.httpclient.Cookie} properties. * * @param attribute {@link org.apache.commons.httpclient.NameValuePair} cookie attribute from the * <tt>Set-Cookie2</tt> header. * @param cookie {@link org.apache.commons.httpclient.Cookie} to be updated * @throws MalformedCookieException if an exception occurs during parsing */ public void parseAttribute(final NameValuePair attribute, final Cookie cookie) throws MalformedCookieException { if (attribute == null) { throw new IllegalArgumentException("Attribute may not be null."); } if (attribute.getName() == null) { throw new IllegalArgumentException("Attribute Name may not be null."); } if (cookie == null) { throw new IllegalArgumentException("Cookie may not be null."); } final String paramName = attribute.getName().toLowerCase(); final String paramValue = attribute.getValue(); CookieAttributeHandler handler = findAttribHandler(paramName); if (handler == null) { // ignore unknown attribute-value pairs if (LOG.isDebugEnabled()) LOG.debug("Unrecognized cookie attribute: " + attribute.toString()); } else { handler.parse(cookie, paramValue); } }
private String send(HttpMethod method, boolean sslExceptionIgnore) throws HttpNetAgentException { String responseBody = null; HttpClient hc = null; try { this.hashCode(); String reqLog = ">> " + (method instanceof PostMethod ? "POST" : "GET") + " [" + method.getURI() + "]"; if (method instanceof PostMethod) { NameValuePair[] params = ((PostMethod) method).getParameters(); for (int i = 0; i < params.length; i++) { NameValuePair param = params[i]; reqLog += "\n" + (param.getName() + "=" + param.getValue()); } } reqLog += ", hscd[" + this.hashCode() + "], sslExcIgnore[" + sslExceptionIgnore + "], hs[" + this.hashCode() + "]"; logger.info(reqLog); logger.info(getHttpInfoDumy(method)); HttpConnectionManager httpConnMgr = new SimpleHttpConnectionManager(); HttpConnectionManagerParams httpConnMgrParams = new HttpConnectionManagerParams(); // Connection Timeout 설정 httpConnMgrParams.setConnectionTimeout(netTimeoutConn); // Socket Timeout 설정 httpConnMgrParams.setSoTimeout(netTimeoutSock); httpConnMgr.setParams(httpConnMgrParams); hc = new HttpClient(httpConnMgr); /* if (sslExceptionIgnore){ try { Protocol.registerProtocol("https", new Protocol("https", new EasySSLProtocolSocketFactory(), 443)); } catch (GeneralSecurityException e) { e.printStackTrace(); } } */ int status = hc.executeMethod(method); if (200 == status) { responseBody = method.getResponseBodyAsString(); logger.info("<< responseBody [" + responseBody + "], hs[" + this.hashCode() + "]"); if (responseBody == null) responseBody = ""; } else { logger.error( "<< http status Not Success [" + status + "], hs[" + this.hashCode() + "]," + getHttpInfoDumy(method)); throw new HttpNetAgentException( HttpNetAgentException.HTTP_HAEDER_NOT_SUCCESS, "Http Status[" + status + "]"); } } catch (HttpException e) { logger.error( "<< HttpException msg [" + e.getMessage() + "], hs[" + this.hashCode() + "]," + getHttpInfoDumy(method), e); throw new HttpNetAgentException(HttpNetAgentException.HTTP_NET_ERROR, e.getMessage()); } catch (IOException e) { logger.error( "<< IOException msg [" + e.getMessage() + "], hs[" + this.hashCode() + "]," + getHttpInfoDumy(method), e); throw new HttpNetAgentException(HttpNetAgentException.HTTP_NET_ERROR, e.getMessage()); } return responseBody; }