Subhasish Dash Subhasish Dash - 2 months ago 39
Java Question

Exception in thread "main" java.net.SocketTimeoutException: Read timed out in jsoup

I am able to reach this link manually and search properly using firefox browser but I am failing to connect using JSOUP.

Code :

String url = "https://www.sosnc.gov/trademrk/search.aspx";
Connection.Response response = Jsoup.connect(url).timeout(45000)
.method(Connection.Method.GET)
.ignoreContentType(true)
.userAgent("Mozilla/5.0 (Windows NT 6.3; rv:40.0) Gecko/20100101 Firefox/40.0")
.followRedirects(true)
.execute();
Map<String, String> loginCookies = response.cookies();
Document document = response.parse(); //search results
Element __VIEWSTATE = document.select("input[name=__VIEWSTATE]").first();
Element __VIEWSTATEGENERATOR = document.select("input[name=__VIEWSTATEGENERATOR]").first();
Element __PREVIOUSPAGE = document.select("input[name=__PREVIOUSPAGE]").first();
Element __EVENTVALIDATION = document.select("input[name=__EVENTVALIDATION]").first();
response = Jsoup.connect(url).timeout(45000)
.data("SosMenu_SiteTreeView_ExpandState", "ennnnnnnnnnnn")
.data("SosMenu_SiteTreeView_PopulateLog", "")
.data("SosMenu_SiteTreeView_SelectedNode", "SosMenu_SiteTreeViewn2")
.data("ToolsTreeView_ExpandState", "ennn")
.data("ToolsTreeView_PopulateLog", "")
.data("ToolsTreeView_SelectedNode", "")
.data("__EVENTARGUMENT", "")
.data("__EVENTTARGET", "")
.data("__EVENTVALIDATION", __EVENTVALIDATION.val())
.data("__PREVIOUSPAGE", __PREVIOUSPAGE.val())
.data("__VIEWSTATE", __VIEWSTATE.val())
.data("__VIEWSTATEGENERATOR", __VIEWSTATEGENERATOR.val())
.data("ctl00$ctl00$SosContent$SosContent$Submit1", "Search")
.data("ctl00$ctl00$SosContent$SosContent$Type", "Goods")
.data("ctl00$ctl00$SosContent$SosContent$txtSearc", query)
.cookies(loginCookies)
.method(Connection.Method.POST)
.ignoreContentType(true)
.userAgent("Mozilla/5.0 (Windows NT 6.3; rv:40.0) Gecko/20100101 Firefox/40.0")
.header("host", "www.sosnc.gov")
.referrer("https://www.sosnc.gov/trademrk/search.aspx")
.followRedirects(true)
.execute();
document = response.parse(); //search results
System.out.println(document);


Am I missing something? It is a Jsoup post request to the server, so I have also added Cookies and the required parameters, but still failing to get results.

TDG TDG
Answer

I don't know why you get a timeout, but you can easily get the data in a much simpler way -

String query = "abc";
String url = "https://www.sosnc.gov/trademrk/results.aspx?searchstr=" +
              query +
             "&Type=GOODS";
Connection.Response response = Jsoup.connect(url).timeout(45000)
            .method(Connection.Method.GET)
            .ignoreContentType(true)
            .userAgent("Mozilla/5.0 (Windows NT 6.3; rv:40.0) Gecko/20100101 Firefox/40.0")

            .followRedirects(true)
            .execute();
System.out.println(response.body());

No cookies or extra parameters are needed.