當前位置: 首頁>>代碼示例>>Java>>正文


Java URL.openConnection方法代碼示例

本文整理匯總了Java中java.net.URL.openConnection方法的典型用法代碼示例。如果您正苦於以下問題:Java URL.openConnection方法的具體用法?Java URL.openConnection怎麽用?Java URL.openConnection使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.net.URL的用法示例。


在下文中一共展示了URL.openConnection方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: doInBackground

import java.net.URL; //導入方法依賴的package包/類
@Override
protected String doInBackground(String... urls) {

    try {
        // zakładamy, że jest tylko jeden URL
        URL url = new URL(urls[0]);
        URLConnection connection = url.openConnection();

        // pobranie danych do InputStream
        InputStream in = new BufferedInputStream(
                connection.getInputStream());

        // konwersja InputStream na String
        // wynik będzie przekazany do metody onPostExecute()
        return streamToString(in);

    } catch (Exception e) {
        // obsłuż wyjątek
        Log.d(MainActivity.class.getSimpleName(), e.toString());
        return null;
    }

}
 
開發者ID:Aifryz,項目名稱:FireHelper,代碼行數:24,代碼來源:MapsActivityCurrentPlace.java

示例2: access

import java.net.URL; //導入方法依賴的package包/類
/** access a url, ignoring some IOException such as the page does not exist */
static void access(String urlstring) throws IOException {
  LOG.warn("access " + urlstring);
  URL url = new URL(urlstring);
  URLConnection connection = url.openConnection();
  connection.connect();
  
  try {
    BufferedReader in = new BufferedReader(new InputStreamReader(
        connection.getInputStream()));
    try {
      for(; in.readLine() != null; );
    } finally {
      in.close();
    }
  } catch(IOException ioe) {
    LOG.warn("urlstring=" + urlstring, ioe);
  }
}
 
開發者ID:naver,項目名稱:hadoop,代碼行數:20,代碼來源:TestGlobalFilter.java

示例3: queryAPI

import java.net.URL; //導入方法依賴的package包/類
public String queryAPI(String query) throws IOException {
    URL url = new URL(API_URL + query);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();

    int responseCode = connection.getResponseCode();
    if (responseCode != 200) {
        throw new IOException("Server responded with: " + responseCode + " " + connection.getResponseMessage());
    }

    InputStream input = connection.getInputStream();
    BufferedReader reader = new BufferedReader(new InputStreamReader(input));
    StringBuilder response = new StringBuilder();
    String line;
    while ((line = reader.readLine()) != null) {
        response.append(line);
    }
    input.close();
    return response.toString();
}
 
開發者ID:lutobler,項目名稱:openmensa-java,代碼行數:20,代碼來源:OpenMensaOrg.java

示例4: getData

import java.net.URL; //導入方法依賴的package包/類
private static String getData(
        String target,
        String method,
        String token)
{
    try {
        URL url = new URL(target);
        HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();

        if (!TextUtils.isEmpty(token)) {
            conn.setRequestProperty("Authorization", token);
        }

        conn.setRequestMethod(method);
        conn.connect();

        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line, data = "";
        while ((line = in.readLine()) != null) {
            data += line;
        }
        in.close();

        return data;
    } catch (IOException ignored) {
        ignored.printStackTrace();
    }

    return null;
}
 
開發者ID:nextgis,項目名稱:android_nextgis_mobile,代碼行數:31,代碼來源:NGIDUtils.java

示例5: post

import java.net.URL; //導入方法依賴的package包/類
/**
 * Submits an HTTP POST request to the given URL.
 *
 * @param url the URL to receive the POST request
 * @return the reply
 * @throws IOException in case of failure
 */
public InputStream post(URL url) throws IOException {
  writeEnd();

  OutputStream out = null;
  try {
    final HttpURLConnection http = (HttpURLConnection) url.openConnection();

    http.setRequestMethod("POST");
    http.setDoInput(true);
    http.setDoOutput(true);
    http.setUseCaches(false);
    http.setAllowUserInteraction(false);

    http.setRequestProperty("Content-Type",
      "multipart/form-data; boundary=" + boundary);
    http.setRequestProperty("Content-Length", String.valueOf(bytes.size()));

    out = http.getOutputStream();
    bytes.writeTo(out);
    out.close();

    return http.getInputStream();
  }
  finally {
    IOUtils.closeQuietly(out);
  }
}
 
開發者ID:ajmath,項目名稱:VASSAL-src,代碼行數:35,代碼來源:HTTPPostBuilder.java

示例6: checkUrl

import java.net.URL; //導入方法依賴的package包/類
/**
* 獲取遠程服務器ATN結果
* @param urlvalue 指定URL路徑地址
* @return 服務器ATN結果
* 驗證結果集:
* invalid命令參數不對 出現這個錯誤,請檢測返回處理中partner和key是否為空 
* true 返回正確信息
* false 請檢查防火牆或者是服務器阻止端口問題以及驗證時間是否超過一分鍾
*/
private static String checkUrl(String urlvalue) {
    String inputLine = "";

    try {
        URL url = new URL(urlvalue);
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection
            .getInputStream()));
        inputLine = in.readLine().toString();
    } catch (Exception e) {
        e.printStackTrace();
        inputLine = "";
    }

    return inputLine;
}
 
開發者ID:superkoh,項目名稱:k-framework,代碼行數:26,代碼來源:AlipayNotify.java

示例7: getAllKfAccount

import java.net.URL; //導入方法依賴的package包/類
/**
 * 獲取所有客服帳號
 *
 * @return
 */
public static String getAllKfAccount() {
    String token = WeiXinUtils.getToken();
    if (token != null) {
        String urlString = "https://api.weixin.qq.com/cgi-bin/customservice/getkflist?access_token=" + token;
        try {
            URL url = new URL(urlString);
            HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection();
            httpsURLConnection.setDoInput(true);
            DataInputStream dataInputStream = new DataInputStream(httpsURLConnection.getInputStream());
            StringBuffer stringBuffer = new StringBuffer();
            int inputByte = dataInputStream.read();
            while (inputByte != -1) {
                stringBuffer.append((char) inputByte);
                inputByte = dataInputStream.read();
            }
            String kfString = stringBuffer.toString();
            return kfString;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    return null;
}
 
開發者ID:guokezheng,項目名稱:automat,代碼行數:29,代碼來源:WeiXinKFUtils.java

示例8: postJson

import java.net.URL; //導入方法依賴的package包/類
public static JSONObject postJson(String url, String data) throws IOException, JSONException {
  //Log.d("postJson " + url+" med data="+data);
  URL u = new URL(url);
  HttpURLConnection urlConnection = (HttpURLConnection) u.openConnection();
  urlConnection.setConnectTimeout(15000);
  urlConnection.setReadTimeout(90 * 1000);   // 1 1/2 minut
  urlConnection.setRequestProperty("Content-Type", "application/json");
  urlConnection.setDoOutput(true);
  urlConnection.connect(); // http://stackoverflow.com/questions/8179658/urlconnection-getcontent-return-null
  OutputStream os = urlConnection.getOutputStream();
  os.write(data.getBytes());
  os.close();
  InputStream is = urlConnection.getInputStream();
  if (urlConnection.getResponseCode() != 200)
    throw new IOException("HTTP-svar var " + urlConnection.getResponseCode() + " " + urlConnection.getResponseMessage() + " for " + u);

  tjekOmdirigering(u, urlConnection);

  return new JSONObject(læsStreng(is));
}
 
開發者ID:nordfalk,項目名稱:EsperantoRadio,代碼行數:21,代碼來源:Diverse.java

示例9: ProxyURLConnection

import java.net.URL; //導入方法依賴的package包/類
public ProxyURLConnection(
  URL url,
  URL proxied
  ) throws IOException
{
  super(url);
  
  _delegate = proxied.openConnection();
}
 
開發者ID:apache,項目名稱:myfaces-trinidad,代碼行數:10,代碼來源:ProxyResourceLoader.java

示例10: getRoomID

import java.net.URL; //導入方法依賴的package包/類
public String getRoomID(String WEBSERVICE_URL, String AUTHTOKEN, String roomName) {
    // Convert config room name to lowercase with no spaces
    roomName = roomName.toLowerCase().replaceAll("\\s+", "");

    try {

        URL url = new URL("http://" + WEBSERVICE_URL + "/data/domain/");
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setConnectTimeout(TIMEOUT);
        con.setRequestMethod(METHOD);
        con.setRequestProperty("SECRET", AUTHTOKEN);

        InputStream response = con.getInputStream();

        try (Scanner scanner = new Scanner(response)) {
            String responseBody = scanner.useDelimiter("\\A").next();
            HeatHub hh = new Gson().fromJson(responseBody, HeatHub.class);
            for (Room myRoom : hh.getRoom()) {
                String myRoomName = myRoom.name.toLowerCase().replaceAll("\\s+", "");
                if (myRoomName.equals(roomName)) {
                    return myRoom.id.toString();
                }
            }
        }
    } catch (IOException e) {
        logger.warn("Communication error occurred while getting your Drayton Wiser information: {}",
                e.getMessage());
    }
    return null;
}
 
開發者ID:RobPope,項目名稱:DraytonWiser,代碼行數:31,代碼來源:DraytonWiserConnection.java

示例11: openConn

import java.net.URL; //導入方法依賴的package包/類
static URLConnection openConn(String urlk) throws MalformedURLException, IOException {  

      URL url = new URL(urlk);  
      HttpURLConnection hsu = (HttpURLConnection) url.openConnection();  
      hsu.setDoInput(true);  
      hsu.setDoOutput(true);  
      hsu.setRequestMethod("POST");  
      return hsu;  

  }
 
開發者ID:hsj-xiaokang,項目名稱:springboot-shiro-cas-mybatis,代碼行數:11,代碼來源:RestFulLogin.java

示例12: testDefaultToPostWhenDoOutput

import java.net.URL; //導入方法依賴的package包/類
@SmallTest
@Feature({"Cronet"})
@CompareDefaultWithCronet
// Regression test for crbug.com/571436.
public void testDefaultToPostWhenDoOutput() throws Exception {
    URL url = new URL(NativeTestServer.getEchoMethodURL());
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setDoOutput(true);
    OutputStream out = connection.getOutputStream();
    out.write("dummy data".getBytes());
    assertEquals(200, connection.getResponseCode());
    assertEquals("OK", connection.getResponseMessage());
    assertEquals("POST", TestUtil.getResponseAsString(connection));
    connection.disconnect();
}
 
開發者ID:lizhangqu,項目名稱:chromium-net-for-android,代碼行數:16,代碼來源:CronetHttpURLConnectionTest.java

示例13: openConnection

import java.net.URL; //導入方法依賴的package包/類
private URLConnection openConnection(URL localURL) throws Exception {
    URLConnection connection;
    if (proxyHost != null && proxyPort != null) {
        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));
        connection = localURL.openConnection(proxy);
    } else {
        connection = localURL.openConnection();
    }
    return connection;
}
 
開發者ID:Fetax,項目名稱:Fetax-AI,代碼行數:11,代碼來源:HttpUtil.java

示例14: createConnection

import java.net.URL; //導入方法依賴的package包/類
private static HttpURLConnection createConnection() throws Exception {
    URL url = new URL(PROFILE_URL);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("POST");
    connection.setRequestProperty("Content-Type", "application/json");
    connection.setUseCaches(false);
    connection.setDoInput(true);
    connection.setDoOutput(true);
    return connection;
}
 
開發者ID:funkemunky,項目名稱:HCFCore,代碼行數:11,代碼來源:UUIDFetcher.java

示例15: extractJarFile

import java.net.URL; //導入方法依賴的package包/類
protected void extractJarFile (URL url, String configuration_folder,
   String dest_folder) throws IOException
{
   final JarURLConnection connection
         = (JarURLConnection) url.openConnection ();
   if (connection != null)
   {
      Enumeration<JarEntry> entries = connection.getJarFile ().entries ();
      while (entries.hasMoreElements ())
      {
         JarEntry entry = (JarEntry) entries.nextElement ();
         if (!entry.isDirectory () && entry.getName ().equals (
               configuration_folder))
         {
            InputStream in = connection.getJarFile ().getInputStream (entry);
            try
            {
               File file =
                     new File (dest_folder);
               if (!file.getParentFile ().exists ())
               {
                  file.getParentFile ().mkdirs ();
               }
               OutputStream out = new FileOutputStream (file);
               try
               {
                  IOUtils.copy (in, out);
               }
               finally
               {
                  out.close ();
               }
            }
            finally
            {
               in.close ();
            }
         }
      }
   }
}
 
開發者ID:SentinelDataHub,項目名稱:dhus-core,代碼行數:42,代碼來源:WebApplication.java


注:本文中的java.net.URL.openConnection方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。