Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java代码类似于;卷曲/grep“;_Java_Curl_Grep - Fatal编程技术网

Java代码类似于;卷曲/grep“;

Java代码类似于;卷曲/grep“;,java,curl,grep,Java,Curl,Grep,在终端中,如果我使用命令 curl -u username:passw -v https://example.com/rest/prototype/1/content/123456 | grep VALUE-I-WANT 它返回我想要的值。 如何在Java中复制它?最简单的方法是使用 例如,要获取Windows上默认浏览器的注册表值,请执行以下操作: String command = "curl -u username:passw -v https://example.com/rest/pro

在终端中,如果我使用命令

curl -u username:passw -v https://example.com/rest/prototype/1/content/123456 | grep VALUE-I-WANT
它返回我想要的值。
如何在Java中复制它?

最简单的方法是使用

例如,要获取Windows上默认浏览器的注册表值,请执行以下操作:

String command = "curl -u username:passw -v https://example.com/rest/prototype/1/content/123456 | grep VALUE-I-WANT";
try
{
    Process process = Runtime.getRuntime().exec(command);
} catch (IOException e)
{
    e.printStackTrace();
}
然后使用
扫描仪
获取命令的输出(如有必要)

Scanner kb = new Scanner(process.getInputStream());

注意
\
字符串
中的转义字符,必须转义才能正常工作。此外,这只是一个帮助参考,而不是预先准备好的实际解决方案。

或者如果您想使用Http客户端点击url,然后解析数据以获得结果,那么除了上面的答案之外

好的方面是,如果你想发布点击/请求,你可以很容易地做到这一点

对于post,您可以使用如下方法

public Map<String, String> postData(String url, Map<String, String> headerMap, Map<String, String> formMap) throws IOException {

        CustomRedirectStratergy customRedirectStratergy = new CustomRedirectStratergy();
        CloseableHttpClient httpClient = null;
        HttpPost httpPost = new HttpPost(url);
        StringBuilder builder = null;

//        LOGGER.info("hiturl {} ",url);

        if (headerMap != null) {
            for (Map.Entry<String, String> entry : headerMap.entrySet()) {
//                LOGGER.info("headers ---- {}, {}",entry.getKey(), entry.getValue());
                httpPost.setHeader(entry.getKey(), entry.getValue());
            }
        }

        if (formMap != null) {
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            for (Map.Entry<String, String> entry : formMap.entrySet()) {
                nameValuePairs.add(new BasicNameValuePair(entry.getKey(), entry
                        .getValue()));
            }
            UrlEncodedFormEntity form = new UrlEncodedFormEntity(nameValuePairs);
            httpPost.setEntity(form);
        }

        httpClient = HttpClientBuilder.create().
                setRedirectStrategy(customRedirectStratergy).
                setDefaultRequestConfig(globalConfig).
                setDefaultCookieStore(cookieStore).
                build();

        HttpResponse response = httpClient.execute(httpPost);
        HttpEntity httpEntity = response.getEntity();
        Charset charset = ContentType.getOrDefault(httpEntity).getCharset();
        if (charset == null) {
            charset = StandardCharsets.UTF_8;
        }

        Map<String, String> resultMap = new HashMap<>();
        resultMap.put(CLIENT_RESPOSNE, EntityUtils.toString(response.getEntity(), charset));
        resultMap.put(LAST_REDIRECT, customRedirectStratergy.getRedirectionLocation());

        return resultMap;
    }
publicmap postData(字符串url、映射头映射、映射表单映射)引发IOException{
CustomRedirectStratergy CustomRedirectStratergy=新CustomRedirectStratergy();
CloseableHttpClient httpClient=null;
HttpPost HttpPost=新的HttpPost(url);
StringBuilder=null;
//info(“hiturl{}”,url);
if(headerMap!=null){
对于(Map.Entry:headerMap.entrySet()){
//info(“头--{},{}”,entry.getKey(),entry.getValue());
setHeader(entry.getKey(),entry.getValue());
}
}
if(formMap!=null){
List nameValuePairs=新的ArrayList();
for(Map.Entry:formMap.entrySet()){
添加(新的BasicNameValuePair(entry.getKey(),entry
.getValue());
}
UrlEncodedFormEntity表单=新的UrlEncodedFormEntity(nameValuePairs);
httpPost.setEntity(表格);
}
httpClient=HttpClientBuilder.create()。
设置重定向策略(customRedirectStratergy)。
setDefaultRequestConfig(globalConfig)。
setDefaultCookieStore(cookieStore)。
build();
HttpResponse response=httpClient.execute(httpPost);
HttpEntity HttpEntity=response.getEntity();
Charset Charset=ContentType.getOrDefault(httpEntity.getCharset();
if(字符集==null){
字符集=标准字符集.UTF_8;
}
Map resultMap=new HashMap();
resultMap.put(CLIENT_RESPOSNE,EntityUtils.toString(response.getEntity(),charset));
结果映射.put(最后一次重定向,customRedirectStratergy.getRedirectionLocation());
返回结果图;
}
很明显,如果存在重定向,您需要覆盖customRedirectStratergy以获取


然后使用任何解析器(可能是JSOUP解析器)做你喜欢做的事情

,虽然这可以从理论上回答问题,但在这里包括答案的基本部分,并提供链接供参考。我目前对该评论没有任何意义。你能重写一下吗?或者解释一下你的意思。。