问题
I am testing a https post on android , I have 2 android devices, this code perfectly worked on my 2.3 device but will be blocked in my 4.1 device at "retcode=urlCon.getResponseCode();", also I have checked the packet in sniffer the post request has been sent and the server did reply the https response,but it still be blocked until timeout.
private String GetHttpPage(HttpsURLConnection urlsCon,String host,String url,String reference,boolean type,String curcookie,String postparam){
String query = postparam;
byte[] entitydata = query.getBytes();
String line="";
HttpURLConnection urlCon=urlsCon;
try{
String domain="";
String fullurl=host+url;
if(urlCon==null){
if(host.indexOf("https")!=-1){
urlCon =GetHttpsConnect(fullurl);
domain=host.substring(8);
}
else{
urlCon =GetHttpConnect(fullurl);
domain=host.substring(7);
}
}
urlCon.setConnectTimeout(30000);
urlCon.setReadTimeout(30000);
if(reference!=null)
urlCon.setRequestProperty("Referer", reference);
urlCon.setRequestProperty("Cache-Control", "no-cache");
urlCon.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)");
urlCon.setRequestProperty("Accept-Encoding", "identity");
urlCon.setRequestProperty("Accept", "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*");
urlCon.setRequestProperty("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
urlCon.setRequestProperty("connection", "Keep-Alive");
urlCon.setRequestProperty("Host", domain);
//
if(curcookie.length()>0)
urlCon.setRequestProperty("Cookie", curcookie);
if(type){
urlCon.setDoOutput(true);
urlCon.setDoInput(true);
urlCon.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
urlCon.setRequestProperty("Content-Length",String.valueOf(entitydata.length));
urlCon.setRequestMethod("POST");
}
else{
urlCon.setDoOutput(false);
urlCon.setDoInput(true);
urlCon.setRequestMethod("GET");
}
urlCon.connect();
if(type){
OutputStream outStream = urlCon.getOutputStream();
outStream.write(entitydata, 0, entitydata.length);
// outStream.write(entitydata);
outStream.flush();
outStream.close();
}
retcode=urlCon.getResponseCode();
if(urlCon.getHeaderField("Location")!=null)
redirect=urlCon.getHeaderField("Location");
Map m=urlCon.getHeaderFields();
Set set=m.entrySet();
Iterator it=set.iterator();
while(it.hasNext())
{
Map.Entry me=(Map.Entry)it.next();
String skey=me.getKey()!=null?me.getKey().toString():"";
String svalue=me.getValue()!=null?me.getValue().toString():"";
if(skey.compareToIgnoreCase("Set-Cookie")==0){
String tempcookie=svalue;
tempcookie=tempcookie.substring(1, tempcookie.length()-1);
cookie+=ProcessCookie(tempcookie);
//cookie=cookie.substring(0, cookie.length()-2);
}
}
// if(urlCon.getHeaderField("Set-Cookie")!=null)
// cookie=urlCon.getHeaderField("Set-Cookie");
BufferedReader in = new BufferedReader(new InputStreamReader(urlCon.getInputStream()));
String subline="";
while ((subline = in.readLine()) != null) {
line+=subline;
}
in.close();
}
catch(IOException e){
String aaa=e.toString();
Log.e("xx", aaa);
}
catch(Exception e){
String aaa=e.toString();
Log.e("xx", aaa);
}
if(urlCon!=null)
urlCon.disconnect();
return line;
}
回答1:
I suppose you have the Internet permission inside the manifest?I think that the problem is in the "User-Agent" inside one of the setRequestProperty or maybe in another one, but not in the rest of the code.
回答2:
Try putting
System.setProperty("http.keepAlive", "false");
somewhere in your code.
来源:https://stackoverflow.com/questions/12284812/https-post-in-android-4-0