Hello, I am using HttpClient-4.5.6 to submit a form and get the HTML output from it. It works on browser as follows; I have a form (NewOnlineForm.aspx) where I have to enter details and the form submits its data using POST method. The request parameter of the form is defined according to the data entered. But it does not return anything. It just creates a cookie (something like ASP.NET_SessionId=xxxxxxxx) and performs redirection (HTTP status code 302). The redirected (PrintSemesterAdmitCard.aspx) page does not have any request parameter, it just takes the cookie (ASP.NET_SessionId=xxxxxxxx) as a request header which is submitted using GET method. Its response is the destination page. Note: Both the source (NewOnlineForm.aspx) and destination page (PrintSemesterAdmitCard.aspx) have request and response headers. The request parameters for the source page are as follows:
__EVENTARGUMENT __EVENTTARGET __EVENTVALIDATION /wEWDgKHm9fuCALcuszrBQKZ2aOHDQKJtonpAQKWtonpAQKXtonpAQLfh7CcDQL9yr/oCAKQ466MBwL7nPWPAwKgt7D9CgL+6YyoCgKWt4DRDgL1qfmOBFJNIkoiLnRIqSkukWuIABMlLB4VqTUK25fA9AcFShKj __LASTFOCUS __VIEWSTATE /wEPDwUKLTI3Mjc2OTQzOQ9kFgICAw9kFjQCBQ8PFgIeBFRleHQFCzEyNjE3MDAzMTIwZGQCCw8QZGQWAQIBZAIPDxBkEBUCCi0tU2VsZWN0LS0DNXRoFQIKLS1TZWxlY3QtLQMzMDgUKwMCZ2cWAQIBZAITDw8WAh8ABQZCLlRlY2hkZAIVDw8WAh8ABQZCLlRlY2hkZAIXDw8WAh8ABQtCLlRlY2gtMjAxN2RkAhkPDxYCHwAFBDIwMTdkZAIbDw8WAh8ABQNZRVNkZAIdDw8WAh8ABSdFbGVjdHJvbmljcyAmIENvbW11bmljYXRpb24gRW5naW5lZXJpbmdkZAIfDw8WAh8ABQNPRERkZAIhDw8WAh8ABQQyMDE5ZGQCIw8PFgIfAGVkZAIlDw8WAh8ABQsyMDE3IC0gMjAxOGRkAicPDxYCHwAFCzIwMTcgLSAyMDIxZGQCKQ8PFgIfAAUGQi5UZWNoZGQCKw8PFgIfAAUKTm90QWxsb3dlZGRk…hAPZBYCAgIPZBYCAgEPEA8WAh8DaGRkZGQCEQ9kFgICAg9kFgICAQ8QDxYCHwNoZGRkZAISD2QWAgICD2QWAgIBDxAPFgIfA2hkZGRkAhMPZBYCAgIPZBYCAgEPEA8WAh8DaGRkZGQCFA9kFgICAg9kFgICAQ8QDxYCHwNoZGRkZAIVD2QWAgICD2QWAgIBDxAPFgIfA2hkZGRkAhYPZBYCAgIPZBYCAgEPEA8WAh8DaGRkZGQCFw9kFgICAg9kFgICAQ8QDxYCHwNoZGRkZAIYD2QWAgICD2QWAgIBDxAPFgIfA2hkZGRkAhkPZBYCAgIPZBYCAgEPEA8WAh8DaGRkZGQCGg9kFgICAg9kFgICAQ8QDxYCHwNoZGRkZAJFDxYCHwJoFgICAg9kFgJmD2QWAgIJDw8WAh8CaGRkAkcPFgIfAmgWAmYPZBYCZg9kFgICBw8PFgIfAWhkZGTGcU3NnybYQwi2UJi9f2t5GUOUgYv+Pd+0FXvHaI3oMw== btnPrintAdmitCard Print+Admit+Card DrRegBack1 1 DrSemSelection 308 ToolkitScriptManager1_HiddenField txtAutoExamRollNoEnter 12617003120 Note: The source page does not contain any response parameter. The request headers for the source (NewOnlineForm.aspx) page are as follows: Host: 136.232.2.202:8083 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: en-US,en;q=0.5 Accept-Encoding: gzip, deflate Referer: http://136.232.2.202:8083/NewOnlineForm.aspx Content-Type: application/x-www-form-urlencoded Content-Length: 2681 Cookie: ASP.NET_SessionId=xuwte3j1orwssxo12qzu30qn Connection: keep-alive Upgrade-Insecure-Requests: 1 The response headers for the source (NewOnlineForm.aspx) page are as follows: Cache-Control private Content-Length 145 Content-Type text/html; charset=utf-8 Date Thu, 12 Dec 2019 14:11:16 GMT Location /PrintSemesterAdmitCard.aspx Server Microsoft-IIS/7.5 X-AspNet-Version 4.0.30319 X-Powered-By ASP.NET Note: The destination page does not contain any request parameter. The request headers for the destination (PrintSemesterAdmitCard.aspx) page are as follows: Host: 136.232.2.202:8083 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: en-US,en;q=0.5 Accept-Encoding: gzip, deflate Referer: http://136.232.2.202:8083/NewOnlineForm.aspx Cookie: ASP.NET_SessionId=xuwte3j1orwssxo12qzu30qn Connection: keep-alive Upgrade-Insecure-Requests: 1 The response headers for the destination (PrintSemesterAdmitCard.aspx) page are as follows: Cache-Control private Content-Length 24127 Content-Type text/html; charset=utf-8 Date Thu, 12 Dec 2019 14:11:17 GMT Server Microsoft-IIS/7.5 X-AspNet-Version 4.0.30319 X-Powered-By ASP.NET This is the entire thing that happens is the browser. Now I want to do the entire thing using apache HttpClient-4.5.6 To use cookie I created a CookieStore object. CookieStore cookieStore=new BasicCookieStore(); Then I created a RequestConfig object to set CookieSpec as DEFAULT RequestConfig globalConfig=RequestConfig.custom().setCookieSpec(CookieSpecs.DEFAULT).build(); Then I created a HttpClientBuilder object to set the default cookie store and the redirect strategy. HttpClientBuilder clientBuilder=HttpClients.custom(); clientBuilder=clientBuilder.setDefaultCookieStore(cookieStore).setDefaultRequestConfig(globalConfig).setRedirectStrategy(new LaxRedirectStrategy()); Then I created a CloseableHttpClient object and built the clientBuilder object. Then I sent request to the source page, added parameters and got the HttpResponse object. But when I tried to print the status line and status code, it returned 200 instead of 302. Then I tried to print the URL after redirection. HttpHost target = context.getTargetHost(); List<URI> redirectLocations = context.getRedirectLocations(); URI location = URIUtils.resolve(httppost.getURI(), target, redirectLocations); System.out.println("Final HTTP location: " + location.toASCIIString()); Unfortunately it returned the prevoius URL (NewOnlineForm.aspx) Then I tried to print all the cookies. List<Cookie> cookies=cookieStore.getCookies(); if(cookies.isEmpty()){ System.out.println("none"); }else{ for(int i=0;i<cookies.size();i++){ System.out.println("- "+cookies.get(i).toString()); } } I got the following output. - [version: 0][name: ASP.NET_SessionId][value: z5mgnvwj2i431rto2pvoay1q][domain: 136.232.2.202][path: /][expiry: null] Then I tried to manually go to the destination page using that cookie and print the response. HttpGet httpget=new HttpGet("http://[ip]:[port]/PrintSemesterAdmitCard.aspx"); context.setAttribute(HttpClientContext.COOKIE_STORE,cookieStore); httpget.setConfig(localConfig); HttpResponse response2=httpclient.execute(httpget,context); Scanner sc=new Scanner(response2.getEntity().getContent()); while(sc.hasNextLine()){ System.out.println(sc.nextLine()); } I got the HTML page but all the values are replaced by the variables. (Note: I got the same result in the browser when I entered an arbitrary ASP.NET_SessionId to the request header of the destination page) I think I need to force the redirection of the source page and get the HTTP status code 302 (In browser the redirection is triggered only after clicking button) Is there any way to it? Please help me to figure it out. (Note: The full source code is attached)
import org.apache.http.Header; import org.apache.http.client.CookieStore; import org.apache.http.impl.client.BasicCookieStore; import org.apache.http.impl.cookie.BasicClientCookie; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClients; import org.apache.http.HttpResponse; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.client.methods.RequestBuilder; import java.net.URI; import java.util.List; import java.util.Iterator; import org.apache.http.HttpHost; import org.apache.http.impl.client.LaxRedirectStrategy; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.config.CookieSpecs; import org.apache.http.client.protocol.HttpClientContext; import org.apache.http.client.utils.URIUtils; import org.apache.http.protocol.HttpContext; import org.apache.http.protocol.HttpCoreContext; import org.apache.http.protocol.BasicHttpContext; import org.apache.http.client.methods.HttpGet; import org.apache.http.cookie.Cookie; import org.apache.http.util.EntityUtils; import java.util.Scanner; public class FormLoginUsingCookie{ public static void main(String[] args) throws Exception{ //Creating a BasicCookieStore object CookieStore cookieStore=new BasicCookieStore(); //Creating a RequestConfig object RequestConfig globalConfig=RequestConfig.custom().setCookieSpec(CookieSpecs.DEFAULT).build(); //Creating a HttpClientBuilder object HttpClientBuilder clientBuilder=HttpClients.custom(); //setDefaultCookieStore to the HttpClientBuilder object clientBuilder=clientBuilder.setDefaultCookieStore(cookieStore).setDefaultRequestConfig(globalConfig).setRedirectStrategy(new LaxRedirectStrategy()); //Create a CloseableHttpClient object CloseableHttpClient httpclient=clientBuilder.build(); try{ RequestConfig localConfig=RequestConfig.copy(globalConfig).setCookieSpec(CookieSpecs.DEFAULT).build(); //Create a RequestBuilder object RequestBuilder requestBuilder=RequestBuilder.post(); requestBuilder=requestBuilder.setUri(new URI("http://136.232.2.202:8083/NewOnlineForm.aspx")); requestBuilder=requestBuilder.addParameter("txtAutoExamRollNoEnter","12617003120").addParameter("DrRegBack1","1").addParameter("DrSemSelection","308").addParameter("btnPrintAdmitCard","Print+Admit+Card"); //requestBuilder=requestBuilder.addParameter("__EVENTARGUMENT","").addParameter("__EVENTTARGET","").addParameter("__EVENTVALIDATION","/wEWDgKHm9fuCALcuszrBQKZ2aOHDQKJtonpAQKWtonpAQKXtonpAQLfh7CcDQL9yr/oCAKQ466MBwL7nPWPAwKgt7D9CgL+6YyoCgKWt4DRDgL1qfmOBFJNIkoiLnRIqSkukWuIABMlLB4VqTUK25fA9AcFShKj").addParameter("__LASTFOCUS","").addParameter("__VIEWSTATE","/wEPDwUKLTI3Mjc2OTQzOQ9kFgICAw9kFjQCBQ8PFgIeBFRleHQFCzEyNjE3MDAzMTIwZGQCCw8QZGQWAQIBZAIPDxBkEBUCCi0tU2VsZWN0LS0DNXRoFQIKLS1TZWxlY3QtLQMzMDgUKwMCZ2cWAQIBZAITDw8WAh8ABQZCLlRlY2hkZAIVDw8WAh8ABQZCLlRlY2hkZAIXDw8WAh8ABQtCLlRlY2gtMjAxN2RkAhkPDxYCHwAFBDIwMTdkZAIbDw8WAh8ABQNZRVNkZAIdDw8WAh8ABSdFbGVjdHJvbmljcyAmIENvbW11bmljYXRpb24gRW5naW5lZXJpbmdkZAIfDw8WAh8ABQNPRERkZAIhDw8WAh8ABQQyMDE5ZGQCIw8PFgIfAGVkZAIlDw8WAh8ABQsyMDE3IC0gMjAxOGRkAicPDxYCHwAFCzIwMTcgLSAyMDIxZGQCKQ8PFgIfAAUGQi5UZWNoZGQCKw8PFgIfAAUKTm90QWxsb3dlZGRk…hAPZBYCAgIPZBYCAgEPEA8WAh8DaGRkZGQCEQ9kFgICAg9kFgICAQ8QDxYCHwNoZGRkZAISD2QWAgICD2QWAgIBDxAPFgIfA2hkZGRkAhMPZBYCAgIPZBYCAgEPEA8WAh8DaGRkZGQCFA9kFgICAg9kFgICAQ8QDxYCHwNoZGRkZAIVD2QWAgICD2QWAgIBDxAPFgIfA2hkZGRkAhYPZBYCAgIPZBYCAgEPEA8WAh8DaGRkZGQCFw9kFgICAg9kFgICAQ8QDxYCHwNoZGRkZAIYD2QWAgICD2QWAgIBDxAPFgIfA2hkZGRkAhkPZBYCAgIPZBYCAgEPEA8WAh8DaGRkZGQCGg9kFgICAg9kFgICAQ8QDxYCHwNoZGRkZAJFDxYCHwJoFgICAg9kFgJmD2QWAgIJDw8WAh8CaGRkAkcPFgIfAmgWAmYPZBYCZg9kFgICBw8PFgIfAWhkZGTGcU3NnybYQwi2UJi9f2t5GUOUgYv+Pd+0FXvHaI3oMw==").addParameter("ToolkitScriptManager1_HiddenField",""); //Create a HttpUriRequest object HttpUriRequest httppost=requestBuilder.build(); //httppost.setConfig(localConfig); HttpClientContext context = HttpClientContext.create(); //Execute the HttpUriRequest object CloseableHttpResponse response=httpclient.execute(httppost,context); try{ //Printing the status line System.out.println(response.getStatusLine()); HttpHost target = context.getTargetHost(); List<URI> redirectLocations = context.getRedirectLocations(); URI location = URIUtils.resolve(httppost.getURI(), target, redirectLocations); System.out.println("Final HTTP location: " + location.toASCIIString()); //Printing the cookies List<Cookie> cookies=cookieStore.getCookies(); System.out.println("List of cookies"); if(cookies.isEmpty()){ System.out.println("none"); }else{ for(int i=0;i<cookies.size();i++){ System.out.println("- "+cookies.get(i).toString()); } } HttpGet httpget=new HttpGet("http://136.232.2.202:8083/PrintSemesterAdmitCard.aspx"); //HttpContext localContext=new BasicHttpContext(); //localContext.getAttribute(HttpCoreContext.HTTP_CONNECTION); context.setAttribute(HttpClientContext.COOKIE_STORE,cookieStore); httpget.setConfig(localConfig); HttpResponse response2=httpclient.execute(httpget,context); Scanner sc=new Scanner(response2.getEntity().getContent()); while(sc.hasNextLine()){ System.out.println(sc.nextLine()); } }finally{ response.close(); } }finally{ httpclient.close(); } } }
--------------------------------------------------------------------- To unsubscribe, e-mail: httpclient-users-unsubscr...@hc.apache.org For additional commands, e-mail: httpclient-users-h...@hc.apache.org