I need to add non standard request header that has format like this:(X-MMP-Params: fs=640x0). I'm using HTTPClient here is the code:
我需要添加非标准请求标头,其格式如下:(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 header right here
HttpResponse responseGet = client.execute(get);
Is this the right way to do that ?
这是正确的方法吗?
1 个解决方案
#1
0
The get request is made with HttpGet:
获取请求是使用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;
}
The POST request is made with HttpPost:
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
}
}
I personally reccoment to try Retrofit in following way
我亲自重新尝试以下列方式进行改造
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);
}
#1
0
The get request is made with HttpGet:
获取请求是使用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;
}
The POST request is made with HttpPost:
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
}
}
I personally reccoment to try Retrofit in following way
我亲自重新尝试以下列方式进行改造
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);
}