我正在尝试为我的请求添加标头,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) 从我的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标题,我的代码中的标题好吗?