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; } }
/** * 运行时,添加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); }
void addBody(HttpURLConnection conn, byte[] content) throws IOException { conn.setRequestProperty(CONTENT_LENGTH, String.valueOf(content.length)); // Set default content type if none is set. if (conn.getRequestProperty(CONTENT_TYPE) == null) { conn.setRequestProperty(CONTENT_TYPE, DEFAULT_CONTENT_TYPE); } conn.setDoOutput(true); conn.getOutputStream().write(content); }
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 ""; }
// добавить продукт в базу 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 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; } }
private byte[] sendHttpMessage(byte[] body) throws IOException { HttpMessageContext context = HttpMessageContext.getInstance(); context.Debug("GNHttpConnection [sendHttpMessage] starts"); if (context.getLogheader()) { context.Info(logMessageSetting()); } connection.setDoInput(true); if (body != null) { connection.setDoOutput(true); OutputStream os = TimedURLConnection.getOutputStream(connection, timeout * 1000); context.Debug("GNHttpConnection [sendHttpMessage] sending message ..."); os.write(body); context.Debug("GNHttpConnection [sendHttpMessage] message sent"); } context.Debug( "GNHttpConnection [sendHttpMessage] TimedURLConnection.getInputStream timeout[" + timeout + " S]"); InputStream is = TimedURLConnection.getInputStream(connection, timeout * 1000); responseCode = connection.getResponseCode(); context.Debug("GNHttpConnection [sendHttpMessage] responseCode[" + responseCode + "]"); responseMessage = HttpMessageContext.getMessage(is); responseheaders = new Hashtable<String, String>(); int no = 0; while (true) { String headerName = connection.getHeaderFieldKey(no); String headerValue = connection.getHeaderField(no); if (headerName != null && headerName.length() > 0) { responseheaders.put(headerName, headerValue); } else { if (headerValue == null || headerValue.length() <= 0) break; } no++; } if (context.getLogheader()) { GTConfigFile head = new GTConfigFile(responseheaders); context.Debug("GNHttpConnection [sendHttpMessage] responseHeader\r\n" + head); context.Debug( "GNHttpConnection [sendHttpMessage] responseMessage\r\n" + new String(getResponseMessage())); context.Info("GNHttpConnection [sendHttpMessage] success for " + url); } else context.Info("GNHttpConnection [sendHttpMessage] success for " + url); return responseMessage; }
/** * 发起查询处理结果请求 * * @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; }
/** * 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; }
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(); }
/** * 登陆百度,其他方法调用之前需要先登陆 * * @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(); } }
@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; }