发送带有标头的 HTTP GET 请求
我想从我的 Android 应用程序请求带有 GET 参数的 URL 并读取响应。 在请求中,我必须添加一个 x-zip
标头。
URL 类似于
http://example.com/getmethod.aspx?id=111&method=Test
Can 有人提供我的代码吗?
有两点很重要:它是 GET 请求并包含 x-zip
header 。
编辑:
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();
}
我尝试使用此代码,但收到带有 .net 错误的代码:对象引用未设置为对象的实例...
我想,但我不确定对于 x-zip
标头,我的代码中的标头是否可以?
From my Android app I want to request a URL with GET parameters and read the response.
In the request I must add a x-zip
header.
The URL is something like
http://example.com/getmethod.aspx?id=111&method=Test
Can some one provide me code for that?
Two things are important: that it is a GET request and contains the x-zip
header .
EDIT:
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();
}
I try with this code but I get code with .net error: Object reference not set to an instance of an object...
I think but I'm not sure this if for x-zip
header, is header in my code ok?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我们在应用程序中用于设置请求标头的代码摘录。您会注意到,我们仅在 POST 或 PUT 上设置 CONTENT_TYPE 标头,但添加标头的一般方法(通过请求拦截器)也用于 GET。
Here's a code excerpt we're using in our app to set request headers. You'll note we set the CONTENT_TYPE header only on a POST or PUT, but the general method of adding headers (via a request interceptor) is used for GET as well.
您完全按照以下行所示进行操作:
所以您的标头很好,问题是 Web 服务的其他一些输入。您需要在服务器端对其进行调试。
You do it exactly as you showed with this line:
So your header is fine and the problem is some other input to the web service. You'll want to debug that on the server side.