相关疑难解决方法(0)

为HttpURLConnection添加标头

我正在尝试为我的请求添加标头,HttpUrlConnection但该方法setRequestProperty()似乎不起作用.服务器端没有收到带有标头的任何请求.

HttpURLConnection hc;
    try {
        String authorization = "";
        URL address = new URL(url);
        hc = (HttpURLConnection) address.openConnection();


        hc.setDoOutput(true);
        hc.setDoInput(true);
        hc.setUseCaches(false);

        if (username != null && password != null) {
            authorization = username + ":" + password;
        }

        if (authorization != null) {
            byte[] encodedBytes;
            encodedBytes = Base64.encode(authorization.getBytes(), 0);
            authorization = "Basic " + encodedBytes;
            hc.setRequestProperty("Authorization", authorization);
        }
Run Code Online (Sandbox Code Playgroud)

java http

233
推荐指数
5
解决办法
35万
查看次数

使用标头发送HTTP GET请求

从我的Android应用程序中,我想要一个带有GET参数的URL并阅读响应.在请求中我必须添加x-zip标头.

URL就像

http://example.com/getmethod.aspx?id=111&method=Test
Run Code Online (Sandbox Code Playgroud)

有人可以为我提供代码吗?

有两件事很重要:它是一个GET请求并包含x-zip标题.

编辑:

try {
    HttpClient client = new DefaultHttpClient();  
    String getURL = "http://example.com/getmethod.aspx?id=111&method=Test";
    HttpGet get = new HttpGet(getURL);
    get.setHeader("Content-Type", "application/x-zip");
    HttpResponse responseGet = client.execute(get);  
    HttpEntity resEntityGet = responseGet.getEntity();  
    if (resEntityGet != null) {  
        //do something with the response
        Log.i("GET ",EntityUtils.toString(resEntityGet));
    }
} catch (Exception e) {
    e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)

我尝试使用此代码,但我得到.net错误的代码:Object reference not set to an instance of an object... 我想但我不确定这是否为x-zip标题,我的代码中的标题好吗?

java android http http-headers

14
推荐指数
2
解决办法
14万
查看次数

标签 统计

http ×2

java ×2

android ×1

http-headers ×1