「curljava调用」curl命令的使用

博主:adminadmin 2022-12-08 00:03:08 51

本篇文章给大家谈谈curljava调用,以及curl命令的使用对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java中轮询3次调用接口怎么做

这是其中一个curl命令:

curl -i -X PUT -d "{'operation':'create_generic_thing','resourceName':‘thing','resourceType':'default'}“

怎么php发送get请求给Java,然后返回想要的具体参数

curl请求java接口,接口返回值后进行相关操作,给你贴一个curl的代码

function ihttp_request($url, $post = '', $extra = array(), $timeout = 60) {

$urlset = parse_url($url);

if (empty($urlset['path'])) {

$urlset['path'] = '/';

}

if (!empty($urlset['query'])) {

$urlset['query'] = "?{$urlset['query']}";

}

if (empty($urlset['port'])) {

$urlset['port'] = $urlset['scheme'] == 'https' ? '443' : '80';

}

if (strexists($url, 'https://')  !extension_loaded('openssl')) {

if (!extension_loaded("openssl")) {

message('请开启您PHP环境的openssl');

}

}

if (function_exists('curl_init')  function_exists('curl_exec')) {

$ch = curl_init();

if (ver_compare(phpversion(), '5.6') = 0) {

curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);

}

if (!empty($extra['ip'])) {

$extra['Host'] = $urlset['host'];

$urlset['host'] = $extra['ip'];

unset($extra['ip']);

}

curl_setopt($ch, CURLOPT_URL, $urlset['scheme'] . '://' . $urlset['host'] . ($urlset['port'] == '80' ? '' : ':' . $urlset['port']) . $urlset['path'] . $urlset['query']);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

@curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

curl_setopt($ch, CURLOPT_HEADER, 1);

@curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);

if ($post) {

if (is_array($post)) {

$filepost = false;

foreach ($post as $name = $value) {

if ((is_string($value)  substr($value, 0, 1) == '@') || (class_exists('CURLFile')  $value instanceof CURLFile)) {

$filepost = true;

break;

}

}

if (!$filepost) {

$post = http_build_query($post);

}

}

curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

}

if (!empty($GLOBALS['_W']['config']['setting']['proxy'])) {

$urls = parse_url($GLOBALS['_W']['config']['setting']['proxy']['host']);

if (!empty($urls['host'])) {

curl_setopt($ch, CURLOPT_PROXY, "{$urls['host']}:{$urls['port']}");

$proxytype = 'CURLPROXY_' . strtoupper($urls['scheme']);

if (!empty($urls['scheme'])  defined($proxytype)) {

curl_setopt($ch, CURLOPT_PROXYTYPE, constant($proxytype));

} else {

curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);

curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);

}

if (!empty($GLOBALS['_W']['config']['setting']['proxy']['auth'])) {

curl_setopt($ch, CURLOPT_PROXYUSERPWD, $GLOBALS['_W']['config']['setting']['proxy']['auth']);

}

}

}

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

curl_setopt($ch, CURLOPT_SSLVERSION, 1);

if (defined('CURL_SSLVERSION_TLSv1')) {

curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);

}

curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1');

if (!empty($extra)  is_array($extra)) {

$headers = array();

foreach ($extra as $opt = $value) {

if (strexists($opt, 'CURLOPT_')) {

curl_setopt($ch, constant($opt), $value);

} elseif (is_numeric($opt)) {

curl_setopt($ch, $opt, $value);

} else {

$headers[] = "{$opt}: {$value}";

}

}

if (!empty($headers)) {

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

}

}

$data = curl_exec($ch);

$status = curl_getinfo($ch);

$errno = curl_errno($ch);

$error = curl_error($ch);

curl_close($ch);

if ($errno || empty($data)) {

return error(1, $error);

} else {

return ihttp_response_parse($data);

}

}

$method = empty($post) ? 'GET' : 'POST';

$fdata = "{$method} {$urlset['path']}{$urlset['query']} HTTP/1.1\r\n";

$fdata .= "Host: {$urlset['host']}\r\n";

if (function_exists('gzdecode')) {

$fdata .= "Accept-Encoding: gzip, deflate\r\n";

}

$fdata .= "Connection: close\r\n";

if (!empty($extra)  is_array($extra)) {

foreach ($extra as $opt = $value) {

if (!strexists($opt, 'CURLOPT_')) {

$fdata .= "{$opt}: {$value}\r\n";

}

}

}

$body = '';

if ($post) {

if (is_array($post)) {

$body = http_build_query($post);

} else {

$body = urlencode($post);

}

$fdata .= 'Content-Length: ' . strlen($body) . "\r\n\r\n{$body}";

} else {

$fdata .= "\r\n";

}

if ($urlset['scheme'] == 'https') {

$fp = fsockopen('ssl://' . $urlset['host'], $urlset['port'], $errno, $error);

} else {

$fp = fsockopen($urlset['host'], $urlset['port'], $errno, $error);

}

stream_set_blocking($fp, true);

stream_set_timeout($fp, $timeout);

if (!$fp) {

return error(1, $error);

} else {

fwrite($fp, $fdata);

$content = '';

while (!feof($fp))

$content .= fgets($fp, 512);

fclose($fp);

return ihttp_response_parse($content, true);

}

}

api数据接口怎么调用

API:应用程序接口(API:Application Program Interface)

应用程序接口(API:application programming interface)是一组定义、程序及协议的集合,通过 API 接口实现计算机软件之间的相互通信。API 的一个主要功能是提供通用功能集。程序员通过使用 API 函数开发应用程序,从而可以避免编写无用程序,以减轻编程任务。

远程过程调用(RPC):通过作用在共享数据缓存器上的过程(或任务)实现程序间的通信。

标准查询语言(SQL):是标准的访问数据的查询语言,通过通用数据库实现应用程序间的数据共享。

文件传输:文件传输通过发送格式化文件实现应用程序间数据共享。

信息交付:指松耦合或紧耦合应用程序间的小型格式化信息,通过程序间的直接通信实现数据共享。

当前应用于 API 的标准包括 ANSI 标准 SQL API。另外还有一些应用于其它类型的标准尚在制定之中。A

java 怎么调用curl,java 怎么调用curl-CSDN问答

java 怎么调用curl,java 怎么调用curl-CSDN问答

java中使用curl命令上传文件的使用方式如下:

curl -F "filename=@/home/test/file.tar.gz"

如果使用了-F参数,curl就会以 multipart/form-data 的方式发送POST请求。-F

java?curl?http请求时间细节,怎么实现

以下代码是Java实现Http的Post、Get、代理访问请求,可以参考一下

package com.snowfigure.kits.net;

 

import java.io.BufferedReader;  

import java.io.IOException;

import java.io.InputStream;  

import java.io.InputStreamReader;  

import java.io.OutputStreamWriter;

import java.io.UnsupportedEncodingException;  

import java.net.HttpURLConnection;  

import java.net.InetSocketAddress;

import java.net.Proxy;

import java.net.URL; 

import java.net.URLConnection;

import java.util.List;

import java.util.Map;

 

/** 

 * Http请求工具类 

 * @author snowfigure 

 * @since 2014-8-24 13:30:56 

 * @version v1.0.1 

 */

public class HttpRequestUtil {

    static boolean proxySet = false;

    static String proxyHost = "127.0.0.1";

    static int proxyPort = 8087;

    /** 

     * 编码 

     * @param source 

     * @return 

     */ 

    public static String urlEncode(String source,String encode) {  

        String result = source;  

        try {  

            result = java.net.URLEncoder.encode(source,encode);  

        } catch (UnsupportedEncodingException e) {  

            e.printStackTrace();  

            return "0";  

        }  

        return result;  

    }

    public static String urlEncodeGBK(String source) {  

        String result = source;  

        try {  

            result = java.net.URLEncoder.encode(source,"GBK");  

        } catch (UnsupportedEncodingException e) {  

            e.printStackTrace();  

            return "0";  

        }  

        return result;  

    }

    /** 

     * 发起http请求获取返回结果 

     * @param req_url 请求地址 

     * @return 

     */ 

    public static String httpRequest(String req_url) {

        StringBuffer buffer = new StringBuffer();  

        try {  

            URL url = new URL(req_url);  

            HttpURLConnection httpUrlConn = (HttpURLConnection) url.openConnection();  

   

            httpUrlConn.setDoOutput(false);  

            httpUrlConn.setDoInput(true);  

            httpUrlConn.setUseCaches(false);  

   

            httpUrlConn.setRequestMethod("GET");  

            httpUrlConn.connect();  

   

            // 将返回的输入流转换成字符串  

            InputStream inputStream = httpUrlConn.getInputStream();  

            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");  

            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);  

   

            String str = null;  

            while ((str = bufferedReader.readLine()) != null) {  

                buffer.append(str);  

            }  

            bufferedReader.close();  

            inputStreamReader.close();  

            // 释放资源  

            inputStream.close();  

            inputStream = null;  

            httpUrlConn.disconnect();  

   

        } catch (Exception e) {  

            System.out.println(e.getStackTrace());  

        }  

        return buffer.toString();  

    }  

       

    /** 

     * 发送http请求取得返回的输入流 

     * @param requestUrl 请求地址 

     * @return InputStream 

     */ 

    public static InputStream httpRequestIO(String requestUrl) {  

        InputStream inputStream = null;  

        try {  

            URL url = new URL(requestUrl);  

            HttpURLConnection httpUrlConn = (HttpURLConnection) url.openConnection();  

            httpUrlConn.setDoInput(true);  

            httpUrlConn.setRequestMethod("GET");  

            httpUrlConn.connect();  

            // 获得返回的输入流  

            inputStream = httpUrlConn.getInputStream();  

        } catch (Exception e) {  

            e.printStackTrace();  

        }  

        return inputStream;  

    }

     

     

    /**

     * 向指定URL发送GET方法的请求

     * 

     * @param url

     *            发送请求的URL

     * @param param

     *            请求参数,请求参数应该是 name1=value1name2=value2 的形式。

     * @return URL 所代表远程资源的响应结果

     */

    public static String sendGet(String url, String param) {

        String result = "";

        BufferedReader in = null;

        try {

            String urlNameString = url + "?" + param;

            URL realUrl = new URL(urlNameString);

            // 打开和URL之间的连接

            URLConnection connection = realUrl.openConnection();

            // 设置通用的请求属性

            connection.setRequestProperty("accept", "*/*");

            connection.setRequestProperty("connection", "Keep-Alive");

            connection.setRequestProperty("user-agent",

                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");

            // 建立实际的连接

            connection.connect();

            // 获取所有响应头字段

            MapString, ListString map = connection.getHeaderFields();

            // 遍历所有的响应头字段

            for (String key : map.keySet()) {

                System.out.println(key + "---" + map.get(key));

            }

            // 定义 BufferedReader输入流来读取URL的响应

            in = new BufferedReader(new InputStreamReader(

                    connection.getInputStream()));

            String line;

            while ((line = in.readLine()) != null) {

                result += line;

            }

        } catch (Exception e) {

            System.out.println("发送GET请求出现异常!" + e);

            e.printStackTrace();

        }

        // 使用finally块来关闭输入流

        finally {

            try {

                if (in != null) {

                    in.close();

                }

            } catch (Exception e2) {

                e2.printStackTrace();

            }

        }

        return result;

    }

 

    /**

     * 向指定 URL 发送POST方法的请求

     * 

     * @param url

     *            发送请求的 URL

     * @param param

     *            请求参数,请求参数应该是 name1=value1name2=value2 的形式。

     * @param isproxy

     *               是否使用代理模式

     * @return 所代表远程资源的响应结果

     */

    public static String sendPost(String url, String param,boolean isproxy) {

        OutputStreamWriter out = null;

        BufferedReader in = null;

        String result = "";

        try {

            URL realUrl = new URL(url);

            HttpURLConnection conn = null;

            if(isproxy){//使用代理模式

                @SuppressWarnings("static-access")

                Proxy proxy = new Proxy(Proxy.Type.DIRECT.HTTP, new InetSocketAddress(proxyHost, proxyPort));

                conn = (HttpURLConnection) realUrl.openConnection(proxy);

            }else{

                conn = (HttpURLConnection) realUrl.openConnection();

            }

            // 打开和URL之间的连接

             

            // 发送POST请求必须设置如下两行

            conn.setDoOutput(true);

            conn.setDoInput(true);

            conn.setRequestMethod("POST");    // POST方法

             

             

            // 设置通用的请求属性

             

            conn.setRequestProperty("accept", "*/*");

            conn.setRequestProperty("connection", "Keep-Alive");

            conn.setRequestProperty("user-agent",

                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");

            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

             

            conn.connect();

             

            // 获取URLConnection对象对应的输出流

            out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");

            // 发送请求参数

            out.write(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;

    }    

     

    public static void main(String[] args) {

        //demo:代理访问

        String url = "";

        String para = "key=youkeyidyouuid=uidadvert_type=intdomain=adf.lyurl=";

         

        String sr=HttpRequestUtil.sendPost(url,para,true);

        System.out.println(sr);

    }

     

}

用java 接收 PHP 发送的xml数据 用curl扩展, 但是java端就是接收不到数据

你先echo 一个值出来看看如echo 'abc';看这边能alert出来吗不过你这个ajax提交,是最原始的xmlhttprequest现在都用jquery9估计很早以前没人直接用xmlhttprequest了建议你用jquery

关于curljava调用和curl命令的使用的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-12-08,除非注明,否则均为首码项目网原创文章,转载请注明出处。