public void run() { try { Thread.sleep(10); byte[] buf = getBuf(); URL url = new URL("http://127.0.0.1:" + port + "/test"); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setDoOutput(true); con.setDoInput(true); con.setRequestMethod("POST"); con.setRequestProperty( "Content-Type", "Multipart/Related; type=\"application/xop+xml\"; boundary=\"----=_Part_0_6251267.1128549570165\"; start-info=\"text/xml\""); OutputStream out = con.getOutputStream(); out.write(buf); out.close(); InputStream in = con.getInputStream(); byte[] newBuf = readFully(in); in.close(); if (buf.length != newBuf.length) { System.out.println("Doesn't match"); error = true; } synchronized (lock) { ++received; if ((received % 1000) == 0) { System.out.println("Received=" + received); } } } catch (Exception e) { // e.printStackTrace(); System.out.print("."); error = true; } }
public static String visitWeb(String urlStr) { URL url = null; HttpURLConnection httpConn = null; InputStream in = null; try { url = new URL(urlStr); httpConn = (HttpURLConnection) url.openConnection(); HttpURLConnection.setFollowRedirects(true); httpConn.setRequestMethod("GET"); httpConn.setRequestProperty("User-Agent", "Mozilla/4.0(compatible;MSIE 6.0;Windows 2000)"); in = httpConn.getInputStream(); return convertStreamToString(in); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { in.close(); httpConn.disconnect(); } catch (Exception ex) { ex.printStackTrace(); } } return null; }
/** * 运行时,添加JVM参数“-Dsun.net.http.retryPost=false”,可阻止自动重连。 * * @see 'http://www.coderanch.com/t/490463/sockets/java/Timeout-retry-URLHTTPRequest' */ @Test public void testConnectionResetByHttpURLConnection() throws IOException { testConnectionResetCount = 0; String resp = null; try { HttpURLConnection conn = (HttpURLConnection) new URL("http://localhost:65532/soso").openConnection(); conn.setDoOutput(true); conn.setRequestMethod("POST"); conn.getOutputStream().write("username".getBytes()); resp = conn.getResponseCode() + ""; } catch (IOException e) { Throwable ee = ExceptionUtils.getRootCause(e); if (ee == null) { ee = e; } Logger.error(this, "", ee); Assert.assertNotSame(NoHttpResponseException.class, ee.getClass()); Assert.assertSame(SocketException.class, ee.getClass()); Assert.assertTrue( "Connection reset".equals(ee.getMessage()) || "Socket closed".equals(ee.getMessage()) || "Unexpected end of file from server".equals(ee.getMessage())); } finally { Logger.info( this, "resp[HttpURLConnection]-[" + testConnectionResetCount + "]=========[" + resp + "]========="); } Assert.assertEquals(2, testConnectionResetCount); }
// прочитать весь json в строку private static String readAll() throws IOException { StringBuilder data = new StringBuilder(); try { HttpURLConnection con = (HttpURLConnection) ((new URL(PRODUCT_URL).openConnection())); con.setRequestMethod("GET"); con.setDoInput(true); String s; try (BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()))) { while ((s = in.readLine()) != null) { data.append(s); } } } catch (MalformedURLException e) { e.printStackTrace(); throw new MalformedURLException("Url is not valid"); } catch (ProtocolException e) { e.printStackTrace(); throw new ProtocolException("No such protocol, it must be POST,GET,PATCH,DELETE etc."); } catch (IOException e) { e.printStackTrace(); throw new IOException("cannot read from server"); } return data.toString(); }
public boolean shutdown(int port, boolean ssl) { try { String protocol = "http" + (ssl ? "s" : ""); URL url = new URL(protocol, "127.0.0.1", port, "shutdown"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setRequestProperty("servicemanager", "shutdown"); conn.connect(); StringBuffer sb = new StringBuffer(); BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8")); int n; char[] cbuf = new char[1024]; while ((n = br.read(cbuf, 0, cbuf.length)) != -1) sb.append(cbuf, 0, n); br.close(); String message = sb.toString().replace("<br>", "\n"); if (message.contains("Goodbye")) { cp.appendln("Shutting down the server:"); String[] lines = message.split("\n"); for (String line : lines) { cp.append("..."); cp.appendln(line); } return true; } } catch (Exception ex) { } cp.appendln("Unable to shutdown CTP"); return false; }
@Test( description = "Verify the downloads links return 200 rather than 404", groups = {"functional"}) public void DownloadsTab_02() throws HarnessException { // Determine which links should be present List<String> locators = new ArrayList<String>(); if (ZimbraSeleniumProperties.zimbraGetVersionString().contains("NETWORK")) { locators.addAll(Arrays.asList(NetworkOnlyLocators)); locators.addAll(Arrays.asList(CommonLocators)); } else if (ZimbraSeleniumProperties.zimbraGetVersionString().contains("FOSS")) { locators.addAll(Arrays.asList(FossOnlyLocators)); locators.addAll(Arrays.asList(CommonLocators)); } else { throw new HarnessException( "Unable to find NETWORK or FOSS in version string: " + ZimbraSeleniumProperties.zimbraGetVersionString()); } for (String locator : locators) { String href = app.zPageDownloads.sGetAttribute("xpath=" + locator + "@href"); String page = ZimbraSeleniumProperties.getBaseURL() + href; HttpURLConnection connection = null; try { URL url = new URL(page); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("HEAD"); int code = connection.getResponseCode(); // TODO: why is 400 returned for the PDF links? // 200 and 400 are acceptable ZAssert.assertStringContains( "200 400", "" + code, "Verify the download URL is valid: " + url.toString()); } catch (MalformedURLException e) { throw new HarnessException(e); } catch (IOException e) { throw new HarnessException(e); } finally { if (connection != null) { connection.disconnect(); connection = null; } } } }
private String getResponse(String link) { try { URL urlObject = new URL(link); HttpURLConnection httpUrlConnection = (HttpURLConnection) urlObject.openConnection(); httpUrlConnection.setRequestMethod("GET"); httpUrlConnection.setDoOutput(true); httpUrlConnection.setRequestProperty("Cookie", cookieValue); return getResponse(httpUrlConnection.getInputStream()); } catch (Exception e) { e.printStackTrace(); } return ""; }
public static boolean hasUpdate(int projectID, String version) { try { HttpURLConnection con = (HttpURLConnection) (new URL("https://api.curseforge.com/servermods/files?projectIds=" + projectID)) .openConnection(); con.setRequestMethod("GET"); con.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; JVM)"); con.setRequestProperty("Pragma", "no-cache"); con.connect(); JSONArray json = (JSONArray) JSONValue.parse(new InputStreamReader(con.getInputStream())); String[] cdigits = ((String) ((JSONObject) json.get(json.size() - 1)).get("name")) .toLowerCase() .split("\\."); String[] vdigits = version.toLowerCase().split("\\."); int max = vdigits.length > cdigits.length ? cdigits.length : vdigits.length; int a; int b; for (int i = 0; i < max; i++) { a = b = 0; try { a = Integer.parseInt(cdigits[i]); } catch (Throwable t1) { char[] c = cdigits[i].toCharArray(); for (int j = 0; j < c.length; j++) { a += (c[j] << ((c.length - (j + 1)) * 8)); } } try { b = Integer.parseInt(vdigits[i]); } catch (Throwable t1) { char[] c = vdigits[i].toCharArray(); for (int j = 0; j < c.length; j++) { b += (c[j] << ((c.length - (j + 1)) * 8)); } } if (a > b) { return true; } else if (a < b) { return false; } else if ((i == max - 1) && (cdigits.length > vdigits.length)) { return true; } } } catch (Throwable t) { } return false; }
public File download(String songId) { try { String maxRate = getMaxRate(songId); JSONObject songInfo = getSongInfo(songId); // 以歌手名字+歌曲名称组成文件名,格式:歌手 - 歌曲名称 String filename = songInfo.getString("artistName") + " - " + songInfo.getString("songName"); String link = "http://yinyueyun.baidu.com/data/cloud/downloadsongfile?songIds=" + songId + "&rate=" + maxRate; URL urlObject = new URL(link); HttpURLConnection httpUrlConnection = (HttpURLConnection) urlObject.openConnection(); httpUrlConnection.setRequestMethod("GET"); httpUrlConnection.setDoOutput(true); httpUrlConnection.setRequestProperty("Cookie", cookieValue); String disposition = httpUrlConnection.getHeaderField("Content-Disposition"); disposition = disposition.replaceAll("\"", ""); // 此转码经测试发现有些是UTF-8编码,有些是GBK编码,所以文件名采用另外方式获得 // disposition = new String(disposition.getBytes("iso-8859-1"),"UTF-8"); // 根据disposition中信息确定歌曲格式 String suffix = disposition.substring(disposition.lastIndexOf(".")); // System.out.println(disposition); InputStream inputStream = httpUrlConnection.getInputStream(); File file = new File(downloadDirectory + "/" + filename + suffix); FileOutputStream fos = new FileOutputStream(file); byte[] buf = new byte[4096]; int read = 0; while ((read = inputStream.read(buf)) > 0) { fos.write(buf, 0, read); } fos.flush(); fos.close(); inputStream.close(); // System.out.println("完成<"+file.getName()+">歌曲下载!"); return file; } catch (Exception e) { e.printStackTrace(); return null; } }
// добавить продукт в базу json server public static void add(ProductREST product) throws NotValidProductException, IOException { try { check(product); // рповеряем продукт // connection к серверу HttpURLConnection con = (HttpURLConnection) ((new URL(PRODUCT_URL).openConnection())); con.setRequestMethod("POST"); // метод post для добавления // генерируем запрос StringBuilder urlParameters = new StringBuilder(); urlParameters .append("name=") .append(URLEncoder.encode(product.getName(), "UTF8")) .append("&"); urlParameters.append("price=").append(product.getPrice()).append("&"); urlParameters.append("weight=").append(product.getWeight()).append("&"); urlParameters .append("manufacturer=") .append(product.getManufacturer().getCountry()) .append("&"); urlParameters.append("category=").append(URLEncoder.encode(product.getCategory(), "UTF8")); con.setDoOutput(true); // разрешаем отправку данных // отправляем try (DataOutputStream out = new DataOutputStream(con.getOutputStream())) { out.writeBytes(urlParameters.toString()); } // код ответа int responseCode = con.getResponseCode(); System.out.println("\nSending 'POST' request to URL : " + PRODUCT_URL); System.out.println("Response Code : " + responseCode); } catch (NotValidProductException e) { e.printStackTrace(); throw e; } catch (UnsupportedEncodingException e) { e.printStackTrace(); throw new UnsupportedEncodingException("cannot recognize encoding"); } catch (ProtocolException e) { e.printStackTrace(); throw new ProtocolException("No such protocol, protocol must be POST,DELETE,PATCH,GET etc."); } catch (MalformedURLException e) { e.printStackTrace(); throw new MalformedURLException("Url is not valid"); } catch (IOException e) { e.printStackTrace(); throw new IOException("cannot write information to server"); } }
public static void main(String args[]) { for (int i = 0; i < args.length; i++) { try { URL u = new URL(args[i]); HttpURLConnection http = (HttpURLConnection) u.openConnection(); http.setRequestMethod("HEAD"); System.out.println(u + "was last modified at " + new Date(http.getLastModified())); } // end try catch (MalformedURLException ex) { System.err.println(args[i] + " is not a URL I understand"); } catch (IOException ex) { System.err.println(ex); } System.out.println(); } // end for } // end main
Response doSend() throws IOException { connection.setRequestMethod(this.verb.name()); if (connectTimeout != null) { connection.setConnectTimeout(connectTimeout.intValue()); } if (readTimeout != null) { connection.setReadTimeout(readTimeout.intValue()); } if (boundary != null) { connection.setRequestProperty(CONTENT_TYPE, "multipart/form-data; boundary=" + boundary); } addHeaders(connection); if (verb.equals(Verb.PUT) || verb.equals(Verb.POST)) { addBody(connection, getByteBodyContents()); } return new Response(connection); }
/** * Get the last modification date of a URL. * * @return last modified timestamp "as is" */ public static long getLastModified(URL url) throws IOException { if ("file".equals(url.getProtocol())) { // Optimize file: access. Also, this prevents throwing an exception if the file doesn't exist // as we try to close the stream below. return new File(URLDecoder.decode(url.getFile(), STANDARD_PARAMETER_ENCODING)).lastModified(); } else { // Use URLConnection final URLConnection urlConnection = url.openConnection(); if (urlConnection instanceof HttpURLConnection) ((HttpURLConnection) urlConnection).setRequestMethod("HEAD"); try { return getLastModified(urlConnection); } finally { final InputStream is = urlConnection.getInputStream(); if (is != null) is.close(); } } }
Response doSend(RequestTuner tuner) throws IOException { connection.setRequestMethod(this.verb.name()); if (connectTimeout != null) { connection.setConnectTimeout(connectTimeout.intValue()); } if (readTimeout != null) { connection.setReadTimeout(readTimeout.intValue()); } tuner.tune(this); addHeaders(connection); if (verb.equals(Verb.PUT) || verb.equals(Verb.POST)) { addBody(connection, getByteBodyContents()); } return new Response(connection); }
private URLConnection prepareConnection(String fileUploadUrl, long fileSize) throws RemoteException, InvalidPropertyFaultMsg, RuntimeFaultFaultMsg, FileFaultFaultMsg, GuestOperationsFaultFaultMsg, InvalidStateFaultMsg, TaskInProgressFaultMsg, MalformedURLException, IOException, ProtocolException { // http://stackoverflow.com/questions/3386832/upload-a-file-using-http-put-in-java URL url = new URL(fileUploadUrl); URLConnection conn = url.openConnection(); conn.setDoInput(true); conn.setDoOutput(true); if (conn instanceof HttpURLConnection) { ((HttpURLConnection) conn).setRequestMethod("PUT"); } else { throw new IllegalStateException("Unknown connection type"); } conn.setRequestProperty("Content-type", "application/octet-stream"); conn.setRequestProperty("Content-length", "" + fileSize); return conn; }
private boolean checkServer(int port, boolean ssl) { try { URL url = new URL("http://127.0.0.1:" + port); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.connect(); int length = conn.getContentLength(); StringBuffer text = new StringBuffer(); InputStream is = conn.getInputStream(); InputStreamReader isr = new InputStreamReader(is); int size = 256; char[] buf = new char[size]; int len; while ((len = isr.read(buf, 0, size)) != -1) text.append(buf, 0, len); isr.close(); if (programName.equals("ISN")) return !shutdown(port, ssl); return true; } catch (Exception ex) { return false; } }
/** * 发起查询处理结果请求 * * @param params 请求参数 * @return 请求结果 * @throws IOException */ public Result getResult(Map<String, Object> params) throws IOException { InputStream is = null; HttpURLConnection conn; StringBuilder sb = new StringBuilder(HOST + "result"); sb.append("?"); for (Map.Entry<String, Object> mapping : params.entrySet()) { sb.append(mapping.getKey() + "=" + mapping.getValue().toString() + "&"); } URL url = new URL(sb.toString().substring(0, sb.length() - 1)); conn = (HttpURLConnection) url.openConnection(); // 设置必要参数 conn.setConnectTimeout(timeout); conn.setRequestMethod("GET"); conn.setUseCaches(false); conn.setDoOutput(true); conn.setRequestProperty("Connection", "Keep-Alive"); conn.setRequestProperty("User-Agent", UpYunUtils.VERSION); // 设置时间 conn.setRequestProperty(DATE, getGMTDate()); // 设置签名 conn.setRequestProperty(AUTHORIZATION, sign(params)); // 创建链接 conn.connect(); // 获取返回的信息 Result result = getResp(conn); if (is != null) { is.close(); } if (conn != null) { conn.disconnect(); } return result; }
protected Void doInBackground(Void... params) { String outString; HttpURLConnection c = null; DataOutputStream os = null; outString = scanData.getOwnBSSID(); outString = outString + "\nL\tX\t" + lastLat + "\t" + lastLon + "\n"; try { URL connectURL = new URL("http://www.openwlanmap.org/android/upload.php"); c = (HttpURLConnection) connectURL.openConnection(); if (c == null) { return null; } c.setDoOutput(true); // enable POST c.setRequestMethod("POST"); c.addRequestProperty("Content-Type", "application/x-www-form-urlencoded, *.*"); c.addRequestProperty("Content-Length", "" + outString.length()); os = new DataOutputStream(c.getOutputStream()); os.write(outString.getBytes()); os.flush(); c.getResponseCode(); os.close(); outString = null; os = null; } catch (IOException ioe) { } finally { try { if (os != null) os.close(); if (c != null) c.disconnect(); } catch (IOException ioe) { ioe.printStackTrace(); } } return null; }
private int[] getNewList(int size, int min, int max) { int[] returnArray = new int[size]; try { URL randomOrg = new URL( "http://www.random.org/cgi-bin/randnum?" + "num=" + size + "&min=" + min + "&max=" + max + "&col=1"); HttpURLConnection con = (HttpURLConnection) randomOrg.openConnection(); con.setRequestMethod("GET"); con.setDoInput(true); InputStream in = con.getInputStream(); InputStreamReader isr = new InputStreamReader(in); BufferedReader br = new BufferedReader(isr); String theLine; for (int i = 0; i < size; i++) { returnArray[i] = Integer.parseInt(br.readLine()); } con.disconnect(); } // if something fails, revert to something a little less random, but still good // the hashCode is based on the objects memory address, which is unique for each object catch (Exception e) { Random r = new Random(); r.setSeed(r.hashCode()); for (int i = 0; i < size; i++) returnArray[i] = r.nextInt(max); } return returnArray; }
/** * When you call this, you post the data, after which it is gone. The post is synchronous. The * function returns after posting and receiving a reply. Get a new data stream with * startDataStream() to make a new post. (You could hold on to the writer, but using it will have * no effect after calling this method.) * * @return HTTP response code, 200 means successful. */ public int postToCDB() throws IOException, ProtocolException, UnsupportedEncodingException { cdbId = -1; if (dataWriter == null) { throw new IllegalStateException("call startDataStream() and write something first"); } HttpURLConnection connection = (HttpURLConnection) postURL.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); dataWriter.flush(); String data = JASPER_DATA_PARAM + "=" + URLEncoder.encode(dataWriter.toString(), "UTF-8"); dataWriter = null; connection.setRequestProperty("Content-Length", "" + Integer.toString(data.getBytes().length)); connection.setUseCaches(false); connection.setDoInput(true); connection.setDoOutput(true); DataOutputStream out = new DataOutputStream(connection.getOutputStream()); out.writeBytes(data); out.flush(); out.close(); int responseCode = connection.getResponseCode(); wasSuccessful = (200 == responseCode); InputStream response; if (wasSuccessful) { response = connection.getInputStream(); } else { response = connection.getErrorStream(); } if (response != null) processResponse(response); connection.disconnect(); return responseCode; }
/** * 登陆百度,其他方法调用之前需要先登陆 * * @param username * @param password */ public void login(String username, String password) { try { URL url = new URL("http://www.baidu.com/"); HttpURLConnection httpUrlConnection = (HttpURLConnection) url.openConnection(); httpUrlConnection.setRequestMethod("GET"); String cookie1 = httpUrlConnection.getHeaderField("Set-Cookie"); // System.out.println("cookie1:"+cookie1); cookie1 = cookie1.substring(0, 45); url = new URL("https://passport.baidu.com/v2/api/?getapi&class=login&tpl=mn&tangram=true"); httpUrlConnection = (HttpURLConnection) url.openConnection(); httpUrlConnection.setRequestMethod("GET"); httpUrlConnection.setRequestProperty("Cookie", cookie1); // httpUrlConnection.connect(); String cookie2 = httpUrlConnection.getHeaderField("Set-Cookie"); System.out.println("cookie2:" + cookie2); cookie2 = cookie2.substring(0, 11); String response = getResponse(httpUrlConnection.getInputStream()); // System.out.println(response); Pattern pattern = Pattern.compile("token='(\\w+)'"); Matcher matcher = pattern.matcher(response); matcher.find(); String token = matcher.group(1); url = new URL("https://passport.baidu.com/v2/api/?login"); httpUrlConnection = (HttpURLConnection) url.openConnection(); httpUrlConnection.setRequestMethod("POST"); // System.out.println(cookie1+"; "+cookie2); httpUrlConnection.setRequestProperty("Cookie", cookie1 + "; " + cookie2); httpUrlConnection.setDoOutput(true); // System.out.println(token); String querystring = "loginType=1&tpl=mn&token=" + token + "&username="******"&password="******"&mem_pass=on"; httpUrlConnection.getOutputStream().write(querystring.getBytes()); httpUrlConnection.getOutputStream().flush(); httpUrlConnection.getOutputStream().close(); response = getResponse(httpUrlConnection.getInputStream()); // System.out.println(response); // String cookie3=httpUrlConnection.getHeaderField("Set-Cookie"); // System.out.println("cookie3:"+cookie3); // 获取登陆后的cookie Map<String, List<String>> hfs = httpUrlConnection.getHeaderFields(); List<String> loginCookies = hfs.get("Set-Cookie"); for (String cookie : loginCookies) { cookieValue += cookie.substring(0, cookie.indexOf(";") + 1); } } catch (Exception e) { e.printStackTrace(); } }
public static String httpPost(String urlAddress, String[] params) { URL url = null; HttpURLConnection conn = null; BufferedReader in = null; StringBuffer sb = new StringBuffer(); try { url = new URL(urlAddress); conn = (HttpURLConnection) url.openConnection(); // 建立连接 // 设置通用的请求属性 /* * conn.setRequestProperty("user-agent", * "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); */ conn.setRequestProperty("User-Agent", "Mozilla/5.0 ( compatible ) "); conn.setRequestProperty("Accept", "*/*"); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); conn.setUseCaches(false); conn.setDoInput(true); // conn.setConnectTimeout(5 * 1000); conn.setDoOutput(true); conn.setRequestMethod("POST"); String paramsTemp = ""; for (String param : params) { if (param != null && !"".equals(param)) { if (params.length > 1) { paramsTemp += "&" + param; } else if (params.length == 1) { paramsTemp = params[0]; } } } byte[] b = paramsTemp.getBytes(); System.out.println("btye length:" + b.length); // conn.setRequestProperty("Content-Length", // String.valueOf(b.length)); conn.getOutputStream().write(b, 0, b.length); conn.getOutputStream().flush(); conn.getOutputStream().close(); int count = conn.getResponseCode(); if (200 == count) { in = new BufferedReader(new InputStreamReader(conn.getInputStream())); // 发送请求 } else { System.out.println("错误类型:" + count); return "server no start-up"; } while (true) { String line = in.readLine(); if (line == null) { break; } else { sb.append(line); } } } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { System.out.println("error ioexception:" + e.getMessage()); e.printStackTrace(); return "server no start-up"; } finally { try { if (in != null) { in.close(); } if (conn != null) { conn.disconnect(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return sb.toString(); }
@Override public HashSet<ScoredAnnotation> solveSa2W(String text) throws AnnotationException { HashSet<ScoredAnnotation> res; try { res = new HashSet<ScoredAnnotation>(); lastTime = Calendar.getInstance().getTimeInMillis(); URL wikiApi = new URL(url); String parameters = "references=true&repeatMode=all&minProbability=0.0&source=" + URLEncoder.encode(text, "UTF-8"); HttpURLConnection slConnection = (HttpURLConnection) wikiApi.openConnection(); slConnection.setRequestProperty("accept", "text/xml"); slConnection.setDoOutput(true); slConnection.setDoInput(true); slConnection.setRequestMethod("POST"); slConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); slConnection.setRequestProperty("charset", "utf-8"); slConnection.setRequestProperty( "Content-Length", "" + Integer.toString(parameters.getBytes().length)); slConnection.setUseCaches(false); DataOutputStream wr = new DataOutputStream(slConnection.getOutputStream()); wr.writeBytes(parameters); wr.flush(); wr.close(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(slConnection.getInputStream()); /* URL wikiApi = new URL(url+"?references=true&repeatMode=all&minProbability=0.0&source="+URLEncoder.encode(text, "UTF-8")); URLConnection wikiConnection = wikiApi.openConnection(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(wikiConnection.getInputStream()); */ lastTime = Calendar.getInstance().getTimeInMillis() - lastTime; XPathFactory xPathfactory = XPathFactory.newInstance(); XPath xpath = xPathfactory.newXPath(); XPathExpression idExpr = xpath.compile("//detectedTopic/@id"); XPathExpression weightExpr = xpath.compile("//detectedTopic/@weight"); XPathExpression referenceExpr = xpath.compile("//detectedTopic/references"); NodeList ids = (NodeList) idExpr.evaluate(doc, XPathConstants.NODESET); NodeList weights = (NodeList) weightExpr.evaluate(doc, XPathConstants.NODESET); NodeList references = (NodeList) referenceExpr.evaluate(doc, XPathConstants.NODESET); for (int i = 0; i < weights.getLength(); i++) { if (weights.item(i).getNodeType() != Node.TEXT_NODE) { int id = Integer.parseInt(ids.item(i).getNodeValue()); float weight = Float.parseFloat(weights.item(i).getNodeValue()); // System.out.println("ID="+ids.item(i).getNodeValue()+" weight="+weight); XPathExpression startExpr = xpath.compile("//detectedTopic[@id=" + id + "]/references/reference/@start"); XPathExpression endExpr = xpath.compile("//detectedTopic[@id=" + id + "]/references/reference/@end"); NodeList starts = (NodeList) startExpr.evaluate(references.item(i), XPathConstants.NODESET); NodeList ends = (NodeList) endExpr.evaluate(references.item(i), XPathConstants.NODESET); for (int j = 0; j < starts.getLength(); j++) { int start = Integer.parseInt(starts.item(j).getNodeValue()); int end = Integer.parseInt(ends.item(j).getNodeValue()); int len = end - start; res.add(new ScoredAnnotation(start, len, id, weight)); } } } } catch (Exception e) { e.printStackTrace(); throw new AnnotationException( "An error occurred while querying Wikipedia Miner API. Message: " + e.getMessage()); } return res; }
protected void setVerb(String verb) throws ProtocolException { connection.setRequestMethod(verb); }