Android 使用HttpUrlConnection发送头值
我尝试使用身份验证值向服务器发送响应,它在登录时提供给我。服务器仅在发送该值时接受请求。但是,它可以很好地与我的代码配合使用,但与我的代码不配合 代码 我也试过这样做Android 使用HttpUrlConnection发送头值,android,get,httpurlconnection,http-token-authentication,Android,Get,Httpurlconnection,Http Token Authentication,我尝试使用身份验证值向服务器发送响应,它在登录时提供给我。服务器仅在发送该值时接受请求。但是,它可以很好地与我的代码配合使用,但与我的代码不配合 代码 我也试过这样做 String token = "AuthToken :" + new String(accessToken); con.setRequestProperty("Authorization", token); 这样试试 String token = new String(new Base64().encode(accessToke
String token = "AuthToken :" + new String(accessToken);
con.setRequestProperty("Authorization", token);
这样试试
String token = new String(new Base64().encode(accessToken.getBytes()));
con.setRequestProperty("AuthToken", token);
这样试试
String token = new String(new Base64().encode(accessToken.getBytes()));
con.setRequestProperty("AuthToken", token);
试试这个
URL url = new URL(strings[0]);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
String token = "" + new String(accessToken);
con.setRequestMethod("GET");
con.setRequestProperty("AuthToken", token);
con.setReadTimeout(15000);
con.setConnectTimeout(15000);
con.setDoOutput(false);
试试这个
URL url = new URL(strings[0]);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
String token = "" + new String(accessToken);
con.setRequestMethod("GET");
con.setRequestProperty("AuthToken", token);
con.setReadTimeout(15000);
con.setConnectTimeout(15000);
con.setDoOutput(false);
尝试替换myURLConnection.setRequestMethod(“GET”);使用myURLConnection.setRequestProperty(“请求方法”、“获取”) 请查找以下代码:
URL myURL = new URL(serviceURL);
HttpURLConnection myURLConnection = (HttpURLConnection)myURL.openConnection();
String userCredentials = "username:password";
String basicAuth = "Basic " + new String(new Base64().encode(userCredentials.getBytes()));
myURLConnection.setRequestProperty ("Authorization", basicAuth);
myURLConnection.setRequestMethod("POST");
myURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
myURLConnection.setRequestProperty("Content-Length", "" + postData.getBytes().length);
myURLConnection.setRequestProperty("Content-Language", "en-US");
myURLConnection.setUseCaches(false);
myURLConnection.setDoInput(true);
myURLConnection.setDoOutput(true);
尝试替换myURLConnection.setRequestMethod(“GET”);使用myURLConnection.setRequestProperty(“请求方法”、“获取”) 请查找以下代码:
URL myURL = new URL(serviceURL);
HttpURLConnection myURLConnection = (HttpURLConnection)myURL.openConnection();
String userCredentials = "username:password";
String basicAuth = "Basic " + new String(new Base64().encode(userCredentials.getBytes()));
myURLConnection.setRequestProperty ("Authorization", basicAuth);
myURLConnection.setRequestMethod("POST");
myURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
myURLConnection.setRequestProperty("Content-Length", "" + postData.getBytes().length);
myURLConnection.setRequestProperty("Content-Language", "en-US");
myURLConnection.setUseCaches(false);
myURLConnection.setDoInput(true);
myURLConnection.setDoOutput(true);
使用这个-
**
* HTTP request types
*/
public static final int POST_TYPE = 1;
public static final int GET_TYPE = 2;
public static final int PUT_TYPE = 3;
public static final int DELETE_TYPE = 4;
/**
* HTTP request header constants
*/
public static final String CONTENT_TYPE = "Content-Type";
public static final String ACCEPT_ENCODING = "Accept-Encoding";
public static final String CONTENT_ENCODING = "Content-Encoding";
public static final String ENCODING_GZIP = "gzip";
public static final String MIME_FORM_ENCODED = "application/x-www-form-urlencoded";
public static final String MIME_TEXT_PLAIN = "text/plain";
private InputStream performRequest(final String contentType, final String url, final String user, final String pass,
final Map<String, String> headers, final Map<String, String> params, final int requestType)
throws IOException {
DefaultHttpClient client = HTTPClientFactory.newClient();
client.getParams().setParameter(HttpProtocolParams.USER_AGENT, mUserAgent);
// add user and pass to client credentials if present
if ((user != null) && (pass != null)) {
client.getCredentialsProvider().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(user, pass));
}
// process headers using request interceptor
final Map<String, String> sendHeaders = new HashMap<String, String>();
if ((headers != null) && (headers.size() > 0)) {
sendHeaders.putAll(headers);
}
if (requestType == HTTPRequestHelper.POST_TYPE || requestType == HTTPRequestHelper.PUT_TYPE ) {
sendHeaders.put(HTTPRequestHelper.CONTENT_TYPE, contentType);
}
// request gzip encoding for response
sendHeaders.put(HTTPRequestHelper.ACCEPT_ENCODING, HTTPRequestHelper.ENCODING_GZIP);
if (sendHeaders.size() > 0) {
client.addRequestInterceptor(new HttpRequestInterceptor() {
public void process(final HttpRequest request, final HttpContext context) throws HttpException,
IOException {
for (String key : sendHeaders.keySet()) {
if (!request.containsHeader(key)) {
request.addHeader(key, sendHeaders.get(key));
}
}
}
});
}
//.... code omitted ....//
}
**
*HTTP请求类型
*/
公共静态最终int POST_TYPE=1;
公共静态最终int GET_TYPE=2;
公共静态最终输入类型=3;
公共静态最终int DELETE_TYPE=4;
/**
*HTTP请求头常量
*/
公共静态最终字符串CONTENT\u TYPE=“CONTENT TYPE”;
公共静态最终字符串ACCEPT\u ENCODING=“ACCEPT ENCODING”;
公共静态最终字符串CONTENT_ENCODING=“CONTENT ENCODING”;
公共静态最终字符串编码\u GZIP=“GZIP”;
公共静态最终字符串MIME\u FORM\u ENCODED=“application/x-www-FORM-urlencoded”;
公共静态最终字符串MIME\u TEXT\u PLAIN=“TEXT/PLAIN”;
私有InputStream performRequest(最终字符串内容类型、最终字符串url、最终字符串用户、最终字符串传递、,
最终映射头、最终映射参数、最终int请求类型)
抛出IOException{
DefaultHttpClient客户端=HTTPClientFactory.newClient();
client.getParams().setParameter(HttpProtocolParams.USER_代理,mUserAgent);
//添加用户和传递到客户端凭据(如果存在)
如果((用户!=null)&&(通过!=null)){
client.getCredentialsProvider().setCredentials(AuthScope.ANY,新用户名PasswordCredentials(user,pass));
}
//使用请求拦截器处理头文件
final Map sendHeaders=新HashMap();
如果((headers!=null)&&(headers.size()>0)){
sendHeaders.putAll(headers);
}
if(requestType==HTTPRequestHelper.POST|u TYPE | requestType==HTTPRequestHelper.PUT_TYPE){
put(HTTPRequestHelper.CONTENT\u类型,contentType);
}
//请求gzip编码进行响应
put(HTTPRequestHelper.ACCEPT_编码,HTTPRequestHelper.ENCODING_GZIP);
如果(sendHeaders.size()>0){
client.addRequestInterceptor(新的HttpRequestInterceptor(){
公共无效进程(最终HttpRequest请求,最终HttpContext上下文)抛出HttpException,
IOException{
for(字符串键:sendHeaders.keySet()){
如果(!request.containsHeader(键)){
request.addHeader(key,sendHeaders.get(key));
}
}
}
});
}
//..代码省略//
}
参考-使用这个-
**
* HTTP request types
*/
public static final int POST_TYPE = 1;
public static final int GET_TYPE = 2;
public static final int PUT_TYPE = 3;
public static final int DELETE_TYPE = 4;
/**
* HTTP request header constants
*/
public static final String CONTENT_TYPE = "Content-Type";
public static final String ACCEPT_ENCODING = "Accept-Encoding";
public static final String CONTENT_ENCODING = "Content-Encoding";
public static final String ENCODING_GZIP = "gzip";
public static final String MIME_FORM_ENCODED = "application/x-www-form-urlencoded";
public static final String MIME_TEXT_PLAIN = "text/plain";
private InputStream performRequest(final String contentType, final String url, final String user, final String pass,
final Map<String, String> headers, final Map<String, String> params, final int requestType)
throws IOException {
DefaultHttpClient client = HTTPClientFactory.newClient();
client.getParams().setParameter(HttpProtocolParams.USER_AGENT, mUserAgent);
// add user and pass to client credentials if present
if ((user != null) && (pass != null)) {
client.getCredentialsProvider().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(user, pass));
}
// process headers using request interceptor
final Map<String, String> sendHeaders = new HashMap<String, String>();
if ((headers != null) && (headers.size() > 0)) {
sendHeaders.putAll(headers);
}
if (requestType == HTTPRequestHelper.POST_TYPE || requestType == HTTPRequestHelper.PUT_TYPE ) {
sendHeaders.put(HTTPRequestHelper.CONTENT_TYPE, contentType);
}
// request gzip encoding for response
sendHeaders.put(HTTPRequestHelper.ACCEPT_ENCODING, HTTPRequestHelper.ENCODING_GZIP);
if (sendHeaders.size() > 0) {
client.addRequestInterceptor(new HttpRequestInterceptor() {
public void process(final HttpRequest request, final HttpContext context) throws HttpException,
IOException {
for (String key : sendHeaders.keySet()) {
if (!request.containsHeader(key)) {
request.addHeader(key, sendHeaders.get(key));
}
}
}
});
}
//.... code omitted ....//
}
**
*HTTP请求类型
*/
公共静态最终int POST_TYPE=1;
公共静态最终int GET_TYPE=2;
公共静态最终输入类型=3;
公共静态最终int DELETE_TYPE=4;
/**
*HTTP请求头常量
*/
公共静态最终字符串CONTENT\u TYPE=“CONTENT TYPE”;
公共静态最终字符串ACCEPT\u ENCODING=“ACCEPT ENCODING”;
公共静态最终字符串CONTENT_ENCODING=“CONTENT ENCODING”;
公共静态最终字符串编码\u GZIP=“GZIP”;
公共静态最终字符串MIME\u FORM\u ENCODED=“application/x-www-FORM-urlencoded”;
公共静态最终字符串MIME\u TEXT\u PLAIN=“TEXT/PLAIN”;
私有InputStream performRequest(最终字符串内容类型、最终字符串url、最终字符串用户、最终字符串传递、,
最终映射头、最终映射参数、最终int请求类型)
抛出IOException{
DefaultHttpClient客户端=HTTPClientFactory.newClient();
client.getParams().setParameter(HttpProtocolParams.USER_代理,mUserAgent);
//添加用户和传递到客户端凭据(如果存在)
如果((用户!=null)&&(通过!=null)){
client.getCredentialsProvider().setCredentials(AuthScope.ANY,新用户名PasswordCredentials(user,pass));
}
//使用请求拦截器处理头文件
final Map sendHeaders=新HashMap();
如果((headers!=null)&&(headers.size()>0)){
sendHeaders.putAll(headers);
}
if(requestType==HTTPRequestHelper.POST|u TYPE | requestType==HTTPRequestHelper.PUT_TYPE){
put(HTTPRequestHelper.CONTENT\u类型,contentType);
}
//请求gzip编码进行响应
put(HTTPRequestHelper.ACCEPT_编码,HTTPRequestHelper.ENCODING_GZIP);
如果(sendHeaders.size()>0){
client.addRequestInterceptor(新的HttpRequestInterceptor(){
公共无效进程(最终HttpRequest请求,最终HttpContext上下文)抛出HttpException,
IOException{
for(字符串键:sendHeaders.keySet()){
如果(!request.containsHeader(键)){
request.addHeader(key,sendHeaders.get(key));
}
}
}
});
}
//..代码省略//
}
参考-参考此代码:-我通过GET方法发送请求参考此代码:-我通过GET方法发送请求我想用HttpUrlConnection执行此操作我想用HttpUrlConnection执行此操作我需要用GET方法执行此操作你可以用GET方法执行此操作我需要用GET方法执行此操作你可以用GET执行相同的操作