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


Java URLConnection.setDoOutput方法代碼示例

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


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

示例1: createRequest

import java.net.URLConnection; //導入方法依賴的package包/類
private static URLConnection createRequest(String strUrl, String strMethod) throws Exception {
	URL url = new URL(strUrl);
	URLConnection conn = url.openConnection();
	conn.setDoInput(true);
	conn.setDoOutput(true);
	if (conn instanceof HttpsURLConnection) {
		HttpsURLConnection httpsConn = (HttpsURLConnection) conn;
		httpsConn.setRequestMethod(strMethod);
		httpsConn.setSSLSocketFactory(getSSLSF());
		httpsConn.setHostnameVerifier(getVerifier());
	} else if (conn instanceof HttpURLConnection) {
		HttpURLConnection httpConn = (HttpURLConnection) conn;
		httpConn.setRequestMethod(strMethod);
	}
	return conn;
}
 
開發者ID:yi-jun,項目名稱:aaden-pay,代碼行數:17,代碼來源:AllinpayXmlTools.java

示例2: shareAndGetResponse

import java.net.URLConnection; //導入方法依賴的package包/類
private String shareAndGetResponse(String selection, int languageDropdownId) throws IOException {
    URL url = new URL("http://pastie.org/pastes");
    URLConnection conn = url.openConnection();
    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    conn.setDoOutput(true);
    OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());

    String data = "paste[parser_id]=" + languageDropdownId +
            "&paste[authorization]=burger&paste[restricted]=1&paste[body]=" + URLEncoder.encode(selection, "UTF-8");
    writer.write(data);
    writer.flush();
    writer.close();

    StringBuffer answer = loadResponse(conn);
    return answer.toString();
}
 
開發者ID:phweda,項目名稱:MFM,代碼行數:17,代碼來源:Pastie.java

示例3: postYbApi

import java.net.URLConnection; //導入方法依賴的package包/類
public String postYbApi(String apiName,String query) throws IOException {
    String url = "https://openapi.yiban.cn/" + apiName;
    String charset = "UTF-8";
    URLConnection connection = new URL(url).openConnection();
    connection.setRequestProperty("Accept-Charset", charset);
    connection.setDoOutput(true);
    connection.setDoInput(true);

    PrintWriter out = new PrintWriter(connection.getOutputStream());
    out.print(query);
    out.flush();
    InputStream response = connection.getInputStream();
    StringBuilder sb=new StringBuilder();
    BufferedReader br = new BufferedReader(new InputStreamReader(response));
    String read;

    while((read=br.readLine()) != null) {
        sb.append(read);
    }
    br.close();
    return sb.toString();
}
 
開發者ID:upcyiban,項目名稱:Integrate,代碼行數:23,代碼來源:QueryService.java

示例4: actionPerformed

import java.net.URLConnection; //導入方法依賴的package包/類
@Override
public void actionPerformed(ActionEvent ae) {
try {
String uname = tf1.getText();
String upwd = tf2.getText();

String url = "http://localhost:7001/loginapp/login?uname="+uname+"&upwd="+upwd;
URL u = new URL(url);
URLConnection uc = u.openConnection();
uc.setDoInput(true);
uc.setDoOutput(true);
InputStream is = uc.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
response = br.readLine();
repaint();
} catch (Exception e) {
e.printStackTrace();
}

}
 
開發者ID:pratikdimble,項目名稱:Servlet_Applet_Communication,代碼行數:21,代碼來源:LoginApplet.java

示例5: getUrlAsString

import java.net.URLConnection; //導入方法依賴的package包/類
public static String getUrlAsString(String url) throws IOException {
  File file = new File(url);
  if (file.isFile()) {
    byte[] encoded = Files.readAllBytes(Paths.get(url));
    return new String(encoded, StandardCharsets.UTF_8);
  }

  URL urlObj = new URL(url);
  URLConnection con = urlObj.openConnection();

  con.setDoOutput(true);
  con.connect();

  BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));

  StringBuilder response = new StringBuilder();
  String inputLine;

  String newLine = System.getProperty("line.separator");
  while ((inputLine = in.readLine()) != null) {
    response.append(inputLine + newLine);
  }

  in.close();

  return response.toString();
}
 
開發者ID:jomof,項目名稱:cdep,代碼行數:28,代碼來源:WebUtils.java

示例6: post

import java.net.URLConnection; //導入方法依賴的package包/類
public String post(String url, String params) {
	String result = "";
	try {
		URL realUrl = new URL(url);
		// 打開和URL之間的連接
		URLConnection conn = realUrl.openConnection();
		// 設置通用的請求屬性
		conn.setRequestProperty("accept", "*/*");
		conn.setRequestProperty("connection", "Keep-Alive");
		// conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible;
		// MSIE 6.0; Windows NT 5.1;SV1)");
		// 發送POST請求必須設置如下兩行
		conn.setDoOutput(true);
		conn.setDoInput(true);

		// 獲取URLConnection對象對應的輸出流
		try (PrintWriter out = new PrintWriter(conn.getOutputStream())) {
			// 發送請求參數
			out.print(params);
			// flush輸出流的緩衝
			out.flush();
		}

		// 定義BufferedReader輸入流來讀取URL的響應
		try (BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()))) {
			String line;
			while ((line = in.readLine()) != null) {
				result += line;
			}
		}
	} catch (Exception e) {
		throw new ZhhrUtilException("發送POST請求出現異常!原因:" + e.getMessage());
	}
	return result;
}
 
開發者ID:wooui,項目名稱:springboot-training,代碼行數:36,代碼來源:HttpUtil.java

示例7: privateApiQuery

import java.net.URLConnection; //導入方法依賴的package包/類
/**
 * Performs a raw private API query.
 * @param method the REST resource
 * @param postParam the JSON to send in the request body.
 * @return result JSON
 */
//Thanks to sampey from https://www.cryptopia.co.nz/Forum/Thread/262
private String privateApiQuery(String method, String postParam) {
    if (secretKey == null) {
        throw new CryptopiaClientException("Please set your secret key before attempting " +
                "to access any private API methods.");
    }
    if (key == null) {
        throw new CryptopiaClientException("Please set your key before attempting " +
                "to access any private API methods.");
    }
    BufferedReader in = null;
    try {
        final String urlMethod = String.format("%s/%s/%s", ROOT_URL, PRIVATE_PATH, method);
        final String nonce = String.valueOf(System.currentTimeMillis());
        final StringBuilder reqSignature = new StringBuilder();
        reqSignature
                .append(key)
                .append("POST")
                .append(URLEncoder.encode(urlMethod, StandardCharsets.UTF_8.toString()).toLowerCase())
                .append(nonce)
                .append(getMD5_B64(postParam));
        final StringBuilder auth = new StringBuilder();
        auth.append("amx ")
                .append(key)
                .append(":")
                .append(sha256_B64(reqSignature.toString()))
                .append(":")
                .append(nonce);
        final URLConnection con = new URL(urlMethod).openConnection();
        con.setDoOutput(true);
        final HttpsURLConnection httpsConn = (HttpsURLConnection) con;
        httpsConn.setRequestMethod("POST");
        httpsConn.setInstanceFollowRedirects(true);
        con.setRequestProperty("Authorization", auth.toString());
        con.setRequestProperty("Content-Type", "application/json");
        final DataOutputStream wr = new DataOutputStream(con.getOutputStream());
        wr.writeBytes(postParam);
        wr.flush();
        wr.close();
        in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuilder response = new StringBuilder();
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        return response.toString();
    }catch (Exception e) {
        throw new CryptopiaClientException("An error occurred communicating with Cryptopia.", e);
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException ignored) {}
        }
    }
}
 
開發者ID:dylanjsa,項目名稱:cryptopia4j,代碼行數:63,代碼來源:CryptopiaClient.java

示例8: completeRequest

import java.net.URLConnection; //導入方法依賴的package包/類
/**
 * Performs any additional processing necessary to complete the request.
 **/
protected void completeRequest( URLConnection connection ) throws IOException {
    ((HttpURLConnection) connection).setRequestMethod( getMethod() );
    connection.setDoInput( true );
    connection.setDoOutput( true );

    OutputStream stream = connection.getOutputStream();
    writeMessageBody( stream );
    stream.flush();
    stream.close();
}
 
開發者ID:parabuild-ci,項目名稱:parabuild-ci,代碼行數:14,代碼來源:MessageBodyWebRequest.java

示例9: getCollectionKeyFromDDS

import java.net.URLConnection; //導入方法依賴的package包/類
public String getCollectionKeyFromDDS( String _collectionName )
{
	String collectionKey = null;

	// make the call to DDS and get ListCollections 
	try { 
		URLConnection connection = new URL ("http://www.dlese.org/dds/services/ddsws1-1?verb=ListCollections" ).openConnection();			
        connection.setDoOutput( true );
        connection.setDoInput(true);
        
        ((HttpURLConnection)connection).setRequestMethod("GET");
        
        Map<String,String> uris = new HashMap<String,String>();
	    uris.put( "ddsws", "http://www.dlese.org/Metadata/ddsws" );	       
        uris.put( "ddswsnews", "http://www.dlese.org/Metadata/ddswsnews" );
	    uris.put( "groups", "http://www.dlese.org/Metadata/groups/" );
	    uris.put( "adn", "http://adn.dlese.org" );
	    uris.put( "annotation", "http://www.dlese.org/Metadata/annotation" );
	    
	    XPath xpath = DocumentHelper.createXPath( "//collections/collection[vocabEntry=\"" + _collectionName + "\"]/searchKey/text()" );

	    xpath.setNamespaceURIs( uris );
	    
	    SAXReader xmlReader = new SAXReader();
	    
	    this.document = xmlReader.read(connection.getInputStream());	
	            
	    Text t = ((Text)xpath.selectSingleNode(this.document));
	    
		collectionKey = t.getStringValue();
		
	} catch ( Exception e ) {
		e.printStackTrace();
	}
	
	return collectionKey;
}
 
開發者ID:NCAR,項目名稱:joai-project,代碼行數:38,代碼來源:CollectionImporter.java

示例10: post

import java.net.URLConnection; //導入方法依賴的package包/類
public static final byte[] post(String url, String data, Map<String, String> headers) {
	try {
		URLConnection conn = new URL(url).openConnection();
		conn.setDoOutput(true);

		if (headers != null) {
			for (Map.Entry<String, String> entry : headers.entrySet()) {
				conn.setRequestProperty(entry.getKey(), entry.getValue());
			}
		}
		OutputStream os = conn.getOutputStream();
		OutputStreamWriter wr = new OutputStreamWriter(os);
		wr.write(data);
		wr.flush();
		wr.close();
		os.close();
		System.out.println("HTTP Response headers: " + conn.getHeaderFields());
		List<String> header = conn.getHeaderFields().get("Content-Disposition");
		if (header != null && header.size() > 0) {
			headers.put("Content-Disposition", header.get(0));
		}
		header = conn.getHeaderFields().get("Content-Type");
		if (header != null && header.size() > 0) {
			headers.put("Content-Type", header.get(0));
		}
		InputStream is = conn.getInputStream();
		byte[] result = IOUtils.toByteArray(is);
		is.close();
		return result;
	} catch (Exception e) {
		throw new RuntimeException(e);
	}
}
 
開發者ID:21ca,項目名稱:selenium-testng-template,代碼行數:34,代碼來源:HttpUtils.java

示例11: setConnectionParameters

import java.net.URLConnection; //導入方法依賴的package包/類
public void setConnectionParameters(URLConnection connection,
    HttpRequestMethod requestMethod) throws ProtocolException {
connection.setRequestProperty("User-Agent", USER_AGENT);
connection.setRequestProperty("Content-Type", JSON_MEDIA_TYPE);
connection.setRequestProperty("Accept", JSON_MEDIA_TYPE);
if (requestMethod.equals(HttpRequestMethod.POST)) {
    connection.setDoOutput(true);
}
   }
 
開發者ID:ahmed-ebaid,項目名稱:invest-stash-rest,代碼行數:10,代碼來源:HttpRequests.java

示例12: doPlipPost

import java.net.URLConnection; //導入方法依賴的package包/類
private String doPlipPost() throws IOException {
    String boundary = Long.toHexString(System.currentTimeMillis());
    String crlf = "\r\n";

    String login = Base64.getEncoder().encodeToString(PLIP_REST_PROVIDER_CREDENTIALS.getBytes());
    URLConnection connection = new URL(plipUrl).openConnection();
    connection.setDoOutput(true);
    connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
    connection.setRequestProperty("Authorization", "Basic " + login);

    OutputStream output = connection.getOutputStream();
    PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, CHARSET), true);
    writer.append("--").append(boundary).append(crlf);
    writer.append("Content-Disposition: form-data; name=\"file\"; filename=\"").append(pdbFilePath.getFileName().toString()).append("\"").append(crlf);
    writer.append("Content-Type: text/plain; charset=" + CHARSET).append(crlf); // Text file itself must be saved in this charset!
    writer.append(crlf).flush();
    Files.copy(pdbFilePath, output);
    output.flush();
    writer.append(crlf).flush();
    writer.append("--").append(boundary).append("--").append(crlf).flush();

    int responseCode = ((HttpURLConnection) connection).getResponseCode();
    logger.info("PLIP REST service response code is " + responseCode);

    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    String outputContent;
    StringBuilder response = new StringBuilder();

    while ((outputContent = in.readLine()) != null) {
        response.append(outputContent);
    }
    in.close();

    return response.toString();
}
 
開發者ID:enauz,項目名稱:mmm,代碼行數:36,代碼來源:PlipPostRequest.java

示例13: completeRequest

import java.net.URLConnection; //導入方法依賴的package包/類
/**
 * Performs any additional processing necessary to complete the request.
 **/
protected void completeRequest( URLConnection connection ) throws IOException {
    super.completeRequest( connection );
    connection.setDoInput( true );
    connection.setDoOutput( true );

    OutputStream stream = connection.getOutputStream();
    writeMessageBody( stream );
    stream.flush();
    stream.close();
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:14,代碼來源:MessageBodyWebRequest.java

示例14: read

import java.net.URLConnection; //導入方法依賴的package包/類
private boolean read() {
    try {
        final URLConnection conn = this.url.openConnection();
        conn.setConnectTimeout(5000);

        if (this.apiKey != null) {
            conn.addRequestProperty("X-API-Key", this.apiKey);
        }
        conn.addRequestProperty("User-Agent", "Updater (by Gravity)");

        conn.setDoOutput(true);

        final BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        final String response = reader.readLine();

        final JSONArray array = (JSONArray) JSONValue.parse(response);

        if (array.size() == 0) {
            this.plugin.getLogger().warning("The updater could not find any files for the project id " + this.id);
            this.result = UpdateResult.FAIL_BADID;
            return false;
        }

        this.versionName = (String) ((JSONObject) array.get(array.size() - 1)).get(Updater.TITLE_VALUE);
        this.versionLink = (String) ((JSONObject) array.get(array.size() - 1)).get(Updater.LINK_VALUE);
        this.versionType = (String) ((JSONObject) array.get(array.size() - 1)).get(Updater.TYPE_VALUE);
        this.versionGameVersion = (String) ((JSONObject) array.get(array.size() - 1)).get(Updater.VERSION_VALUE);

        return true;
    } catch (final IOException e) {
        if (e.getMessage().contains("HTTP response code: 403")) {
            this.plugin.getLogger().warning("dev.bukkit.org rejected the API key provided in plugins/Updater/config.yml");
            this.plugin.getLogger().warning("Please double-check your configuration to ensure it is correct.");
            this.result = UpdateResult.FAIL_APIKEY;
        } else {
            this.plugin.getLogger().warning("The updater could not contact dev.bukkit.org for updating.");
            this.plugin.getLogger().warning("If you have not recently modified your configuration and this is the first time you are seeing this message, the site may be experiencing temporary downtime.");
            this.result = UpdateResult.FAIL_DBO;
        }
        e.printStackTrace();
        return false;
    }
}
 
開發者ID:Borlea,項目名稱:EchoPet,代碼行數:44,代碼來源:Updater.java

示例15: sendPost

import java.net.URLConnection; //導入方法依賴的package包/類
/**
 * 向指定 URL 發送POST方法的請求
 * 
 * @param url
 *            發送請求的 URL
 * @param param
 *            請求參數,請求參數應該是 name1=value1&name2=value2 的形式。
 * @return 所代表遠程資源的響應結果
 */
public static String sendPost(String url, String param) {
    PrintWriter out = null;
    BufferedReader in = null;
    String result = "";
    try {
        URL realUrl = new URL(url);
        // 打開和URL之間的連接
        URLConnection conn = realUrl.openConnection();
        // 設置通用的請求屬性
        conn.setRequestProperty("accept", "*/*");
        conn.setRequestProperty("connection", "Keep-Alive");
        conn.setRequestProperty("user-agent",
                "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
        // 發送POST請求必須設置如下兩行
        conn.setDoOutput(true);
        conn.setDoInput(true);
        // 獲取URLConnection對象對應的輸出流
        out = new PrintWriter(conn.getOutputStream());
        // 發送請求參數
        out.print(param);
        // flush輸出流的緩衝
        out.flush();
        // 定義BufferedReader輸入流來讀取URL的響應
        in = new BufferedReader(
                new InputStreamReader(conn.getInputStream()));
        String line;
        while ((line = in.readLine()) != null) {
            result += line;
        }
    } catch (Exception e) {
        System.out.println("發送 POST 請求出現異常!"+e);
        e.printStackTrace();
    }
    //使用finally塊來關閉輸出流、輸入流
    finally{
        try{
            if(out!=null){
                out.close();
            }
            if(in!=null){
                in.close();
            }
        }
        catch(IOException ex){
            ex.printStackTrace();
        }
    }
    return result;
}
 
開發者ID:cuiods,項目名稱:WIFIProbe,代碼行數:59,代碼來源:HttpRequestUtil.java


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