Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.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
在JavaREST中为路径参数添加值?_Java_Rest_Post_Parameters_Httpurlconnection - Fatal编程技术网

在JavaREST中为路径参数添加值?

在JavaREST中为路径参数添加值?,java,rest,post,parameters,httpurlconnection,Java,Rest,Post,Parameters,Httpurlconnection,简而言之,我尝试添加参数“scan_id”值,但由于这是一篇文章,我无法直接在url路径中添加该值 使用我已有的代码,我将如何修改或添加url以使其正确,也就是说,使其接受我的帖子 不知何故,我一直找不到任何能帮助我弄清楚我将如何着手做这件事的例子 我知道如何使用有效载荷进行POST,如何使用参数进行GET。但是一篇关于Params的帖子让我很困惑 谢谢你的帮助。(我希望继续使用HttpUrlConnection,除非提供了另一个示例,该示例还告诉我如何发送请求,而不仅仅是配置路径 我试着把它加

简而言之,我尝试添加参数“scan_id”值,但由于这是一篇文章,我无法直接在url路径中添加该值

使用我已有的代码,我将如何修改或添加url以使其正确,也就是说,使其接受我的帖子

不知何故,我一直找不到任何能帮助我弄清楚我将如何着手做这件事的例子

我知道如何使用有效载荷进行POST,如何使用参数进行GET。但是一篇关于Params的帖子让我很困惑

谢谢你的帮助。(我希望继续使用HttpUrlConnection,除非提供了另一个示例,该示例还告诉我如何发送请求,而不仅仅是配置路径

我试着把它加到有效载荷上。 我尝试过UriBuilder,但发现它令人困惑,并且与我的代码的其余部分形成了对比,因此我想寻求有关HttpUrlConnection的帮助

NOTICE UPDATE!!

The problem got solved and i added my own answer in the thread
localhost/scans/launch, 
localhost/scans//launch, 
localhost/scans/?/launch, 
localhost/scans/{scan_id}/launch,
例外情况:

URL url = new URL("http://localhost/scans/{scan_id}/launch");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setRequestMethod("POST");
        con.setRequestProperty("tmp_value_dont_mind_this", "432432");
        con.setRequestProperty("X-Cookie", "token=" + "43432");
        con.setRequestProperty("X-ApiKeys", "accessKey="+"43234;" + " secretKey="+"43234;");

        con.setDoInput(true);
        con.setDoOutput(true); //NOT NEEDED FOR GETS
        con.setRequestMethod("POST");
        con.setRequestProperty("Accept", "application/json");
        con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");

                //First example of writing (works when writing a payload)
        OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream(), "UTF-8");
        writer.write(payload);
        writer.close();     

        //second attemp at writing, doens't work (wanted to replace {scan_id} in the url)
        DataOutputStream writer = new DataOutputStream(con.getOutputStream());
        writer.writeChars("scan_id=42324"); //tried writing directly
        //writer.write(payload);
        writer.close();     
我想要三个响应代码中的一个,因为我知道Url是正确的:

Exception in thread "main" java.io.IOException: Server returned HTTP response code: 400 for URL: http://localhost/scans/launch
我试过几个网址

200 Returned if the scan was successfully launched. 
403 Returned if the scan is disabled. 
404 Returned if the scan does not exist. 
如前所述,解决方案是将内容类型更改为
application/x-www-form-urlencoded
,但由于您已经在使用
application/json;charset=UTF-8
(我假设这是您项目的一项要求),因此您没有选择重新设计整个内容。我建议您采取以下措施之一:

  • 添加另一个GET服务
  • 添加另一个内容类型为
    application/x-www-form-urlencoded
    的邮政服务
  • 将此服务替换为上述服务之一
  • (不知道在java中是否可能)
如果我不知道还有其他解决方案,我不知道它们在多大程度上符合HTTP协议


希望我能帮上忙!

为什么不这样使用。由于需要使用
HttpURLConnection
进行
POST
,因此需要在打开连接后将参数写入连接

NOTICE UPDATE!!

The problem got solved and i added my own answer in the thread
localhost/scans/launch, 
localhost/scans//launch, 
localhost/scans/?/launch, 
localhost/scans/{scan_id}/launch,
或者,如果您最后启动了
launch
,只需将上述代码更改为以下代码

String urlParameters  = "scan_id=42324";
byte[] postData       = urlParameters.getBytes(StandardCharsets.UTF_8);

DataOutputStream dataOutputStream  = new DataOutputStream(conn.getOutputStream());
dataOutputStream.write(postData);
这句话在我看来很奇怪;似乎你正试图使用一个URL,在那里你想要一个用户的行为

确切的语法将取决于您选择的模板实现;使用的实现可能如下所示:

URL url = new URL("http://localhost/scans/{scan_id}/launch");
导入org.springframework.web.util.UriTemplate;
导入java.net.url;
//警告-前面有未测试的代码
UriTemplate模板=新的UriTemplate(“http://localhost/scans/{scan_id}/launch”);
Map uriVariables=Collections.singletonMap(“scan_id”,“42324”);
URI=template.expand(uriVariables);
URL=uri.toURL();

因此,在朋友和在座各位的帮助下,我解决了我的问题

下面的代码是一个完整类中的所有代码,一点一点地解释。在底部有一个完整的类及其所有语法等,它接受参数并返回字符串

在HTTP请求中有某些部分。 在我的例子中,这些部分包括请求头、Url中的参数和负载

根据API,API所需的某些变量需要归入各自的类别

import org.springframework.web.util.UriTemplate;
import java.net.url;

// Warning - UNTESTED code ahead
UriTemplate template = new UriTemplate("http://localhost/scans/{scan_id}/launch");
Map<String,String> uriVariables = Collections.singletonMap("scan_id", "42324");
URI uri = template.expand(uriVariables);
URL url = uri.toURL();
我调用的API在有效负载中需要一个名为“format”的参数,并带有一个值

My ORIGINAL URL looked like this: "http://host:port/scans/{scan_id}/export?{history_id}"
I CHANGED to: "https://host:port/scans/" + scan_Id + "/export?history_id=" + ID;
因此,使用我的新URL,我打开了一个连接并设置了需要设置的请求头

String payload = "{\"format\" : \"csv\"}";
发出GET请求时,应注释掉setDoOutput

        HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
在这里,我写入有效负载

        con.setDoInput(true);
        con.setDoOutput(true); 
        con.setRequestMethod("POST");
        con.setRequestProperty("Accept", "application/json");
        con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
        con.setRequestProperty("X-Cookie", "token=" + token);
        con.setRequestProperty("X-ApiKeys", "accessKey="+"23243;" +"secretKey="+"45543;");
在我编写了有效负载之后,我读取了我得到的任何响应(这取决于调用,当我下载文件(get请求)时,我没有要读取的响应,因为我已经通过另一段代码读取了响应)

我希望这有助于任何人谁可能遇到这个线程

        //WRITING THE PAYLOAD to the http call
        OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream(), "UTF-8");
        writer.write(payload);
        writer.close();

请参阅是否使用路径参数?
scan\u id=42324
是一个查询参数。在您的情况下,路径参数应为
http://localhost/scans/42324/launch
。我相信我希望42324出现在查询中。当我发送路径时,API不理解它,并使用400响应代码进行响应。我想我将进一步研究whet她:我可以更改内容类型,也可以如上所述将其删除,并在我了解更多信息后返回:)感谢您的帮助。我更改了内容类型,但不幸的是,我在开始定义URL时仍然存在问题。我以前只使用localhost/scans和localhost/session访问了API,但是一旦有参数,我就找不到API调用,即使它在我的代码顶部被指定。我对这个问题没有太多的经验,但我不排除这是由于应用服务器和/或project maven设置不正确导致的,因为只有有效的url才被发布。我想核实一下。这是一个外部资源,所以它是如何发布的确实是一个好问题。但不管扫描是否存在,我总是要设法让url被接受。但我真的很感谢你的帮助:)考虑到我会这样做,原始URI会是什么样子localhost/scans/launch,-localhost/scans//launch,-localhost/scans/?/launch,-localhost/scans/{scan\u id}/launch,在添加上述代码(尝试了这两种代码)并更改url后仍然以某种方式失败。。我不确定我做错了什么,因为我一直在拿400分。。我只能怀疑这个URL,因为它与localhost/session上的帖子一起工作,但在localhost/session上不起作用。如果我之前没有明确说明,我想补充一点,即提供的url是localhost/scans/{scan_id}/launch,其中localhost是正确主机的替换,因为它是私有的。我尝试在spring中使用spring库实现您的示例,它告诉我无法从UriTemplate转换为url(UriTemplate源于springframework)。还有什么需要注意的吗?老实说,我在当前的实现中走了一点下坡路