Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.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如何发送带有标头的get请求?_Java_C#_Http Headers - Fatal编程技术网

java如何发送带有标头的get请求?

java如何发送带有标头的get请求?,java,c#,http-headers,Java,C#,Http Headers,大家好,我正在尝试用java发送带有标题的get请求。我正在寻找像conn.addHeader(“key”、“value”)这样的方法,但找不到它。我尝试了“setRequestProperty”方法,但它不起作用 public void sendGetRequest(String token) throws MalformedURLException, IOException { // Make a URL to the web page URL url = new URL("

大家好,我正在尝试用java发送带有标题的get请求。我正在寻找像conn.addHeader(“key”、“value”)这样的方法,但找不到它。我尝试了“setRequestProperty”方法,但它不起作用

public void sendGetRequest(String token) throws MalformedURLException, IOException {
    // Make a URL to the web page
    URL url = new URL("http://api.somewebsite.com/api/channels/playback/HLS");

    // Get the input stream through URL Connection
    URLConnection con = url.openConnection();

    //add request header
    con.setRequestProperty("User-Agent", USER_AGENT);
    con.setRequestProperty("Cache-Control", "no-cache");
    con.setRequestProperty("Authorization", "bearer " + token);

    InputStream is = con.getInputStream();

    BufferedReader br = new BufferedReader(new InputStreamReader(is));

    String line = null;

    // read each line and write to System.out
    while ((line = br.readLine()) != null) {
        System.out.println(line);
    }
}
它返回Httpresponse 401错误

我的办公室同事用unity c#发送get请求头,他的代码看起来像是休止符

  JsonData jsonvale = JsonMapper.ToObject(reqDataGet);
        // Debug.Log(jsonvale["access_token"].ToString());
        // /*
        string url = "http://api.somewebsite.com/api/channels/playback/HLS";
        var request = new HTTPRequest(new Uri(url), HTTPMethods.Get, (req, resp) =>
        {
            switch (req.State)
            {
                case HTTPRequestStates.Finished:
                    if (resp.IsSuccess)
                    {
                    }
                    break;
            }

        });
        request.AddHeader("Cache-Control", "no-cache");
        request.AddHeader("Authorization", "bearer " + jsonvale["access_token"].ToString());
        request.Send();

有什么帮助吗?

在Java中,我想您需要这样的东西

    String url = "http://www.google.com/search?q=stackoverflow";

    URL obj = new URL(url);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();

    // optional default is GET
    con.setRequestMethod("GET");

    //add request header
    con.setRequestProperty("User-Agent", "My Example" );

    int responseCode = con.getResponseCode();

在Java中,我想您需要这样的东西

    String url = "http://www.google.com/search?q=stackoverflow";

    URL obj = new URL(url);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();

    // optional default is GET
    con.setRequestMethod("GET");

    //add request header
    con.setRequestProperty("User-Agent", "My Example" );

    int responseCode = con.getResponseCode();