Android 如何将请求头添加到HttpClient请求?
我需要添加格式如下的非标准请求头:(X-MMP-Params:fs=640x0)。 我正在使用HTTPClient,下面是代码:Android 如何将请求头添加到HttpClient请求?,android,http-headers,Android,Http Headers,我需要添加格式如下的非标准请求头:(X-MMP-Params:fs=640x0)。 我正在使用HTTPClient,下面是代码: HttpClient client = new DefaultHttpClient(); String getURL = "http://example.com"; HttpGet get = new HttpGet(getURL); get.setHeader("X-MMP-Params","fs=640x0"); // I set my request hea
HttpClient client = new DefaultHttpClient();
String getURL = "http://example.com";
HttpGet get = new HttpGet(getURL);
get.setHeader("X-MMP-Params","fs=640x0"); // I set my request header right here
HttpResponse responseGet = client.execute(get);
这样做对吗 get请求是通过HttpGet发出的:
public static InputStream getInputStreamFromUrl(String url) {
InputStream content = null;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(new HttpGet(url));
content = response.getEntity().getContent();
} catch (Exception e) {
Log.("[GET REQUEST]", "Network exception", e);
}
return content;
}
POST请求是由
HttpPost:
public void postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "12345"));
nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
public void postData(){
//创建一个新的HttpClient和Post头
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://www.yoursite.com/script.php");
试一试{
//添加您的数据
List nameValuePairs=新的ArrayList(2);
添加(新的BasicNameValuePair(“id”,“12345”);
添加(新的BasicNameValuePair(“stringdata”,“AndDev很酷!”);
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
//执行HTTP Post请求
HttpResponse response=httpclient.execute(httppost);
}捕获(客户端协议例外e){
//TODO自动生成的捕捉块
}捕获(IOE异常){
//TODO自动生成的捕捉块
}
}
我个人建议尝试改装
依循
public interface YourUsersApi {
//You can use rx.java for sophisticated composition of requests
@GET("/users/{user}")
public Observable<SomeUserModel> fetchUser(@Path("user") String user);
//or you can just get your model if you use json api
@GET("/users/{user}")
public SomeUserModel fetchUser(@Path("user") String user);
//or if there are some special cases you can process your response manually
@GET("/users/{user}")
public Response fetchUser(@Path("user") String user);
}
公共接口YourUsersApi{
//您可以使用rx.java进行复杂的请求组合
@获取(“/users/{user}”)
公共可观察fetchUser(@Path(“user”)字符串user);
//或者,如果您使用json api,您也可以直接获取您的模型
@获取(“/users/{user}”)
公共SomeUserModel fetchUser(@Path(“user”)字符串user);
//或者,如果有一些特殊情况,您可以手动处理您的响应
@获取(“/users/{user}”)
公共响应fetchUser(@Path(“user”)字符串user);
}
它能工作吗?要进行测试,您可以设置用户代理
标题,并下载一个显示服务器检索到的用户代理的页面(或创建自己的PHP页面)。如果显示的用户代理是您设置的,那么这个方法是有效的。对于那些徘徊在这个问题上并认为“嗯,这是正确的方法吗?!”的人,答案是肯定的。这很好用。你是否意识到你;至少对于Android项目来说不是这样。