java发送带有标头的HTTP GET请求
从我的Android应用程序中,我想请求一个带有GET参数的URL并读取响应。
在请求中,我必须添加一个x-zip
头
URL类似于
http://example.com/getmethod.aspx?id=111&method=Test
有人能给我提供代码吗
有两件事很重要:它是一个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();
}
我尝试使用此代码,但得到的代码是。净错误:Object reference not set to an instance of an object...
我想,但我不确定对于x-zip
头,我的代码中的头是否正确
# 1 楼答案
您完全按照这一行所示进行操作:
因此,您的头很好,问题是web服务的其他一些输入。您需要在服务器端调试它
# 2 楼答案
下面是我们在应用程序中用来设置请求头的代码摘录。您会注意到,我们仅在POST或PUT上设置CONTENT_-TYPE头,但GET也使用添加头的一般方法(通过请求拦截器)