IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 网络协议 -> 记由HttpUrlConnection的不同实现的底层差异引起的访问错误 -> 正文阅读

[网络协议]记由HttpUrlConnection的不同实现的底层差异引起的访问错误

编写的类SourceCode用于发送网络请求,主要用HttpUrlConnection实现。同一段代码,在电脑上能运行,移植到安卓应用却运行失败。在电脑上运行响应200正常,在手机应用运行时通过打印得知响应码为405,但没有任何错误。
开始时我以为是应用网络权限问题、安卓网络安全机制问题,甚至是url写错了,后来都排除了。
405错误一般是请求方法错误,服务端只接收post请求而我们发送get请求,或相反。我发送的是get请求,打开的网址是网站首页,这个页面是不可能用post请求才能打开的。分析代码,又没发现什么问题,发送的确实是get请求,con.setRequestMethod(“GET”)。真让人摸不着头脑。我之前也是做过安卓的联网应用的,没出现这种问题。
这时,网络上的一句话引起了我的注意,“setDoOutput(true)可能导致请求变为POST请求”。我对于打开的url连接先进行了统一设置,其中正包含“setDoOutput(true)”。在发送get请求的函数中设置setDoOutput(false),果然成功了。
通过打印,得知在手机上url.openConnection()得到的连接的实现类是com.android.okhttp.internal.huc.HttpURLConnectionImpl/HttpsURLConnectionImpl,而电脑上的实现类是原装的sun.net.www.protocol.http.HttpURLConnection/HttpsURLConnectionImpl

结论:对于HttpUrlConnection,

即使设置了setRequestMethod(“GET”),在setDoOutput(true)的情况下也可能会被认为是POST请求

,并且该情况与两个set方法的先后顺序无关。所以 在用HttpURLConnection发送GET请求时,一定要设置setDoOutput(false)



import com.report.auto.multi.AutoRetry;
import com.report.auto.multi.WatchedThread;

import javax.net.ssl.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class SourceCode {
	StringBuffer sourceCode = null;

	public SourceCode() {
	    init();
	}
	private void getHeaders(HttpURLConnection coon){
		Map<String, List<String>> headerFields = coon.getHeaderFields();
		Set<String> strings = headerFields.keySet();
	    for(String key:strings){
			System.out.println(key+"="+headerFields.get(key));
		}
	}
	public StringBuffer getSourceCodeInGet(String url,boolean canJump) {
		try {
			HttpURLConnection httpUrlCon;
			httpUrlCon = getConnection(url);
			httpUrlCon.setRequestMethod("GET");
			httpUrlCon.setDoOutput(false);
			handleHeader(httpUrlCon);
			//Headers(httpUrlCon);
			int responseCode=httpUrlCon.getResponseCode();
			switch (responseCode){
				case 302: case 301:
					if(!canJump)return null;
					System.out.println("跳转");
					//跳转
					String newUrl=httpUrlCon.getHeaderField("Location");
			        if(newUrl==null)newUrl=httpUrlCon.getHeaderField("location");
					newUrl=getFullUrl(url,newUrl);
					if(newUrl==null)return null;
					return getSourceCodeInGet(newUrl,canJump);
				case 404: case 403:return null;
				case 200:
					return getContent(httpUrlCon);
			}
		} catch (IOException e) {
			System.err.println(url);
			e.printStackTrace();
			//userAgent=(userAgent+1)%userAgents.length;
		    //出现其他异常则返回
		}
		return null;
	}
	public String getFullUrl(String sourceUrl,String path){
		if(path.startsWith("http://")||path.startsWith("https://"))return path;
		try {
		int protocolIndex = sourceUrl.indexOf("//")+2;
	    int siteIndex=sourceUrl.indexOf("/",protocolIndex);
		String site;
			if(siteIndex!=-1)site=sourceUrl.substring(0,siteIndex);
			else site=sourceUrl+"/";
		return site+path;
		}catch (Exception e){}
		return null;
	}
	public StringBuffer getSourceCodeInPost(String url,String data){
		StringBuffer sourceCode = new StringBuffer();
		HttpURLConnection httpUrlCon;
		httpUrlCon = getConnection(url);
		PrintWriter printWriter = null;
		//关键代码 application/x-www-form-urlencoded
		try {
			httpUrlCon.setRequestMethod("POST");
			httpUrlCon.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
			//输出表单数据
			printWriter = new PrintWriter(httpUrlCon.getOutputStream());
			printWriter.write(data);
			printWriter.flush();
		    printWriter.close();
		    //处理响应头
            handleHeader(httpUrlCon);
			int responseCode=httpUrlCon.getResponseCode();
			switch (responseCode){
				case 302: case 301:
					//跳转
					String newUrl=httpUrlCon.getHeaderField("location");
					if(newUrl==null)newUrl=httpUrlCon.getHeaderField("Location");
					getSourceCodeInGet(newUrl,true);
					break;
				case 404: case 403:return null;
				case 200: return getContent(httpUrlCon);
			}
		    //获取响应内容
			//return getContent(httpUrlCon);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	Charset charset=StandardCharsets.UTF_8;
    public StringBuffer getContent(HttpURLConnection connection) throws IOException {
		BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), charset));
		String line;
		StringBuffer sourceCode=new StringBuffer();
		while ((line = reader.readLine()) != null) {
			sourceCode.append(line).append("\n");
		}
		reader.close();
		//if(tryTimes!=0) System.out.println("重试"+(tryTimes+1)+"次后找到文件");
		this.sourceCode=sourceCode;
		return sourceCode;
	}
	private void handleHeader(HttpURLConnection connection){
		addCookie(connection.getHeaderField("Set-Cookie"));

	}
	 int userAgent=2;
	static String[] userAgents={"Chrome/71.0.3578.98 Safari/537.36",
			"Mozilla/4.0 compatible; MSIE 5.0;Windows NT;",
			"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36"};
	public HttpURLConnection getConnection(String urlStr) {
		URL url;
		HttpURLConnection conn = null;
		try {
			url = new URL(urlStr);
			conn = (HttpURLConnection) url.openConnection();
			conn.setRequestProperty("user-agent",userAgents[userAgent]);
			conn.setRequestProperty("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
			conn.setRequestProperty("accept-language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");
			//conn.setRequestProperty("accept-encoding", "gzip, deflate, br");
			conn.setRequestProperty("cache-control","max-age=0");
			conn.setRequestProperty("connection", "keep-alive");
			conn.setRequestProperty("cookie",getCookieString());
			conn.setRequestProperty("upgrade-insecure-requests", "1");
			conn.setRequestMethod("GET");
		//	conn.setSSLSocketFactory(getSSLFactory());
			conn.setDoOutput(true);
			conn.setDoInput(true);
			conn.setInstanceFollowRedirects(false);
			conn.setReadTimeout(60*1000);
			conn.setConnectTimeout(60*1000);
			if(conn instanceof HttpsURLConnection){
				((HttpsURLConnection)conn).setSSLSocketFactory(getSSLFactory());
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		setRequestHeader(conn,initHeader);
		System.out.println(conn.getClass());
		return conn;
	}
	String initHeader=null;
	public void setRequestHeader(HttpURLConnection connection,String text){
		if(text==null)return;
		String[] lines = text.split("\n");
	    for(String line:lines){
			if(line.startsWith(":"))line=line.substring(1);
			String[] split = line.split(":");
		    if(split.length!=2)continue;
			connection.setRequestProperty(split[0].trim(),split[1].trim());
		}
	}

	public void setInitHeader(String initHeader) {
		this.initHeader = initHeader;
	}

	Map<String,String>cookieMap=new HashMap<>();
	public void addCookie(String set_cookie){
		if(set_cookie==null)return;
		 String[] cookies=set_cookie.split(";");
	     for(String c:cookies){
	     	if(c.contains("=")) {
				String[] split = c.split("=");
				if(!split[0].trim().equals("path"))
			    cookieMap.put(split[0].trim(),split[1].trim());
	     	}
		 }
	}
	public SSLSocketFactory getSSLFactory(){
		try {
		SSLContext sslContext=SSLContext.getInstance("SSL");
		TrustManager[] tm={new X509TrustManager(){

			@Override
			public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {

			}

			@Override
			public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {

			}

			@Override
			public X509Certificate[] getAcceptedIssuers() {
				return new X509Certificate[0];
			}
		}};
		//初始化
		sslContext.init(null, tm, new java.security.SecureRandom());
		//获取SSLSocketFactory对象
		SSLSocketFactory ssf=sslContext.getSocketFactory();
		return ssf;
		}catch ( NoSuchAlgorithmException | KeyManagementException e){

		}
		return null;
	}
	public String getCookieString(){
		StringBuilder cookie= new StringBuilder();
		for(String key: cookieMap.keySet()){
			cookie.append(key).append("=").append(cookieMap.get(key)).append(";");
		}
		//System.out.println(cookie.toString());
		return cookie.toString();
	}
	private void init() {

	}
	public void clearCookies(){
		cookieMap.clear();
	}
    public String getHtmlWithAutoRetry(String url,int time){
		AutoRetry<String> autoRetry=new AutoRetry<String>() {
			@Override
			public WatchedThread<String> getNewThread() {
				return new WatchedThread<String>() {
					@Override
					public void callBack(String result) {
					}
					@Override
					public String runTask() {
						StringBuffer sourceCodeInGet = getSourceCodeInGet(url,true);
						if(sourceCodeInGet==null)return null;
						return sourceCodeInGet.toString();
					}
				};
			}
		};
		autoRetry.setRetryTimes(time);
		return autoRetry.runForUsableResult();
	}
	int retryTime=3;
	public String getHtmlWithAutoRetry(String url){
		return getHtmlWithAutoRetry(url,retryTime);
	}

	public void setRetryTime(int retryTime) {
		this.retryTime = retryTime;
	}

	public static void main(String[] args) {
		System.out.println(new SourceCode().getSourceCodeInGet("http://www.baidu.com", true));
	}
package com.report.auto.multi;


public abstract class AutoRetry<T>{
    int retryTimes=3;//尝试的最大次数
    boolean retryOnNullResult=true;
    int retrySpace=5000;//重试的时间间隔
    protected WatchedThread<T> thread;
    public T runForUsableResult(){
        boolean shouldRetry=false;
        int time=0;
        do {
            thread=getNewThread();
            if(thread==null)return null;
            thread.start();
            boolean b=thread.waitUntilOK();//是否不超时
            time++;
            shouldRetry=(!b||(retryOnNullResult&&thread.getResult()==null))&&time<retryTimes;
            if(shouldRetry)sleep(retrySpace);
        }while (shouldRetry);
        return thread.getResult();
    }
    /**产生可运行的线程*/
    public abstract WatchedThread<T> getNewThread();
    private void sleep(int time){
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public int getRetryTimes() {
        return retryTimes;
    }

    public void setRetryTimes(int retryTimes) {
        this.retryTimes = retryTimes;
    }

    public boolean isRetryOnNullResult() {
        return retryOnNullResult;
    }

    public void setRetryOnNullResult(boolean retryOnNullResult) {
        this.retryOnNullResult = retryOnNullResult;
    }

    public int getRetrySpace() {
        return retrySpace;
    }

    public void setRetrySpace(int retrySpace) {
        this.retrySpace = retrySpace;
    }
}

package com.report.auto.multi;

public abstract class WatchedThread<T> extends Thread{
    volatile boolean isOK=false;
    int timeOut=60000;
    long startTime;
    volatile boolean isGivenUp=false;
    T result;
    public WatchedThread(){
    }
    public boolean isOK() {
        return isOK;
    }
    @Override
    public synchronized void start() {
        super.start();
        startTime=System.currentTimeMillis();
    }
    public boolean isTimeOut(){
        return System.currentTimeMillis()-startTime>=timeOut;
    }
    public abstract T runTask();
    public abstract void callBack(T result);
    @Override
    public  void run(){
        T t = runTask();
        result=t;
        if(!isGivenUp) {
            callBack(t);
        }
        isOK=true;
    }
    private void  sleep(int time){
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    /**由其他类调用,造成其所在运行线程阻塞
     * @return 不超时返回true,超时返回false*/
    public boolean waitUntilOK(){
        while(!isOK){
            sleep(10);
            if(isTimeOut()){
                isGivenUp=true;
                this.interrupt();
                return false;
            }
        }
        return true;
    }
    public T getResult() {
        return result;
    }
}

.

  网络协议 最新文章
使用Easyswoole 搭建简单的Websoket服务
常见的数据通信方式有哪些?
Openssl 1024bit RSA算法---公私钥获取和处
HTTPS协议的密钥交换流程
《小白WEB安全入门》03. 漏洞篇
HttpRunner4.x 安装与使用
2021-07-04
手写RPC学习笔记
K8S高可用版本部署
mySQL计算IP地址范围
上一篇文章      下一篇文章      查看所有文章
加:2022-04-04 12:47:24  更:2022-04-04 12:50:04 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/26 4:44:39-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码