本文整理汇总了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;
}
}
示例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);
}
}
示例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();
}
示例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;
}
示例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);
}
}
示例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;
}
示例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;
}
示例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));
}
示例9: ProxyURLConnection
import java.net.URL; //导入方法依赖的package包/类
public ProxyURLConnection(
URL url,
URL proxied
) throws IOException
{
super(url);
_delegate = proxied.openConnection();
}
示例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;
}
示例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;
}
示例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();
}
示例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;
}
示例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;
}
示例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 ();
}
}
}
}
}