⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 websession.java

📁 Software Testing Automation Framework (STAF)的开发代码
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
                     }catch (NullPointerException e)        {            throw new InvalidElementIDException (formID, "Form " + formID);        }    }/*****************************************************************************//*                                                                           *//* Method: resetForm                                                         *//* Description: Reset the specified form in the current page.  If the form   *//*              is not in the page or an invalid id type was specified       *//*              specified throw an InvalidElementException.                  *//* Parameters: formID - the identified for the form                          *//*             idType - the type of id used to identify the form             *//*                      ID_ID_TYPE INDEX_ID_TYPE and NAME_ID_TYPE are        *//*                      accepted                                             *//* Returns: void                                                             *//*                                                                           *//*****************************************************************************/        public void resetForm(String formID, int idType)                             throws InvalidElementIDException    {                    WebForm form = findForm(formID, idType);                try         {            form.reset();                    }catch (NullPointerException e)        {            throw new InvalidElementIDException (formID, "Form " + formID);        }    }/*****************************************************************************//*                                                                           *//* Method: setRequestHeader                                                  *//* Description: Set the request to include the header specified.  If the     *//*              header is already part of the request it is replaced by the  *//*              new value.  If it is not present it is added.                *//* Parameters: request - this is the request for the header                  *//*             header - this is the header to be set                         *//* Returns: void                                                             *//*                                                                           *//*****************************************************************************/        protected void setRequestHeader(HttpMethodBase request,                                       Header header)     {        // this is a case insensitive match        Header h = request.getRequestHeader(header.getName());                if (h == null)            request.addRequestHeader(header);        else            request.setRequestHeader(header);    }/*****************************************************************************//*                                                                           *//* Method: requestMethod                                                     *//* Description: submit the http request described                            *//* Parameters: data - hash map containing the components of the request      *//* Returns: void                                                             *//*                                                                           *//*****************************************************************************/        public void requestMethod(HashMap data)throws MalformedURLException,         IOException, FileNotFoundException, STAFException                                                           {        requestMethod((String)data.get(REQUEST_URL),                       (String)data.get(REQUEST_METHOD),                      (HashMap)data.get(REQUEST_HEADERS),                       (Vector)data.get(REQUEST_PARAMETERS),                      (Vector)data.get(REQUEST_FILES),                       (String)data.get(REQUEST_CONTENT),                       (Boolean)data.get(REQUEST_REDIRECT));    }/*****************************************************************************//*                                                                           *//* Method: requestMethod                                                     *//* Description: submit the http request described                            *//* Parameters: targetURL - the target url for this request                   *//*             method - the method of the http request                       *//*             headers - the headers to submit with the http request         *//*             params - the parameters to submit with the http request       *//*             files - a list of file names and associed parameter names to  *//*                     submit with the http request                          *//*             content - the body of the http request if no files or         *//*                       parameters were specified                           *//* Returns: void                                                             *//*                                                                           *//*****************************************************************************/        public void requestMethod(String targetURL, String method,                                       HashMap headers, Vector params,                                      Vector files, String content,                                      Boolean redirect) throws        MalformedURLException, IOException, FileNotFoundException,        STAFException    {        requestMethod(targetURL, method, headers, params, files, content,                      redirect, null, null);    }    public void requestMethod(String targetURL, String method,                                       HashMap headers, Vector params,                                      Vector files, String content,                                      Boolean redirect,                                      String toFile, String toMachine)    throws MalformedURLException, IOException, FileNotFoundException,        STAFException    {        // clean up the url and make it absolute        targetURL = resolveUrl(targetURL);                // check to see if pre-emptive authentication is possible.        session.getParams().setAuthenticationPreemptive(false);        // strip off the protocol        String host = targetURL.substring(targetURL.indexOf("://") + 3);                Iterator it = authenticationSets.keySet().iterator();                // match the current host with one in the list of automatic authenticators        //  if it exists        while(it.hasNext())        {            String key = (String) it.next();            if (host.startsWith(key))            try            {                session.getParams().setAuthenticationPreemptive(true);                String [] values = getAuthenticationSetValues(key);                Credentials creds = new NTCredentials                                        (values[0], values[1], key, values[2]);                //session.getState().setCredentials(null, key, creds);                    session.getState().setCredentials(new AuthScope(                    key, AuthScope.ANY_PORT, AuthScope.ANY_REALM), creds);            }            catch (InvalidElementIDException e)            {                /* do nothing this won't be thrown since key is gotten from a list of                existing keys */            }                        }                releaseConnection();                // purge expired cookies        java.util.Date now = new java.util.Date();        CookieAccess.purgeExpiredCookies(now, session);                // create request        try{            if (method.equalsIgnoreCase("GET"))            {                lastRequest = new GetMethod (targetURL);                // allow for parameters                if (content == null)                    content = "";                content = "?" + content;            }            else if (method.equalsIgnoreCase("HEAD"))                lastRequest = new HeadMethod (targetURL);            else if (method.equalsIgnoreCase("TRACE"))                lastRequest = new TraceMethod (targetURL);            else if (method.equalsIgnoreCase("OPTIONS"))                lastRequest = new OptionsMethod (targetURL);            else if (method.equalsIgnoreCase("DELETE"))                lastRequest = new DeleteMethod (targetURL);            else if (method.equalsIgnoreCase("PUT"))                lastRequest = new PutMethod (targetURL);            else if (method.equalsIgnoreCase("POST"))                lastRequest = new PostMethod (targetURL);        }catch (java.lang.IllegalArgumentException e)        {            // - when URI is invalid            throw new MalformedURLException("Invalid URI " + targetURL);        }         catch(java.lang.IllegalStateException e)        {            //- when protocol of the absolute URI is not recognised            throw new MalformedURLException("Invalid protocol " + targetURL);        }                        if (lastRequest == null)            throw new MalformedURLException("Bad Method");                // set default headers        it = defaultHeaders.keySet().iterator();        while(it.hasNext())         {            String key = (String) it.next();            String value = (String) defaultHeaders.get(key);            setRequestHeader(lastRequest, new Header(key, value));        }                    // set requested headers        if (headers != null)        {                it = headers.keySet().iterator();                        while(it.hasNext())             {                String key = (String) it.next();                String value = (String) headers.get(key);                setRequestHeader(lastRequest, new Header(key, value));            }        }                    // Add parameters        NameValuePair[] pairs = null;        if (params != null)        {            int numParams = params.size();            pairs = new NameValuePair[numParams];            for (int i = 0; i < numParams; i++)            {                Vector pair = (Vector)params.elementAt(i);                String key = (String)pair.elementAt(0);                String value = STAFUtil.removePrivacyDelimiters(                    (String)pair.elementAt(1));                if (PostMethod.class == lastRequest.getClass())                {                    ((PostMethod)lastRequest).addParameter(key, value);                }                else                {                    // Need to url encode key and value                    pairs[i] = new NameValuePair (key, value);                     }            }        }        // Add files, parameters, or content        try        {            if (files != null)            {                // This indicates a Multipart POST method                // The Multipart POST method uses a different request body                // format (e.g. MultipartRequestEntity) than a POST method.                int numFiles = files.size();                Part[] parts = new Part[numFiles];                // Add files to parts array                for (int i = 0; i < numFiles; i++)                {                    Vector pair = (Vector)files.elementAt(i);                    String key = (String)pair.elementAt(0);                    String value = (String)pair.elementAt(1);                    // Note: The key is not currently used.                    // I think Blake used a key in case support for                    // CONTENTFILEMACHINE was added in the future so that the                    // key could be used to match a CONTENTFILE option to a                    // CONTENTFILEMACHINE option.                                        try                    {                        parts[i] = new FilePart(value, new File(value));                    }                    catch (FileNotFoundException e)                    {                        // Error in getting file to be added to a post                        String errMsg = " specified for INPUT FILE form " +                            "control:\n";                        if (value.equals(""))                            errMsg = "No value" + errMsg;                        else                            errMsg = "Invalid value" + errMsg;                                                throw new FileNotFoundException(                            errMsg + "  <INPUT type=\"file\" name=\"" + key +                            "\" value=

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -