开发者社区> 问答> 正文

java HTTPConnt模拟post 提交文件的问题:报错

写了个模拟登陆 oschina的java代码,我现在发带图片的动弹,写不出来了。我模拟不来post提交的图片地址文件,求大神帮帮我。谢谢!
写模拟登陆不是为了发垃圾动弹,只做技术研究!谢谢

@开源中国首席骨科主任   @红薯  @铂金小鸟

/**
	 * HTTP提交
	 * @throws IOException 
	 */
	public static Cookie getHTTPPost(HTTPParameter httPParameter) throws Exception{
		Cookie cookie=new Cookie();
		URL url2 = new URL(httPParameter.getUrl());
		HttpURLConnection connection = (HttpURLConnection) url2.openConnection();
		connection.setDoOutput(true);// 打开写入属性
		connection.setDoInput(true);// 打开读取属性
		connection.setReadTimeout(3000);
		connection.setConnectTimeout(10000);// 连接超时时间 设置连接主机超时(单位:毫秒)
		connection.setReadTimeout(10000);// 设置从主机读取数据超时(单位:毫秒
		connection.setInstanceFollowRedirects(false);// 设置是否重定向
		if(httPParameter.getReferer()!=null){
			connection.addRequestProperty("Referer", httPParameter.getReferer());
		}
		
		if(httPParameter.getCookie()!=null){
			connection.addRequestProperty("Cookie", httPParameter.getCookie());
		}
		if(httPParameter.getHost()!=null){
			connection.addRequestProperty("Host", httPParameter.getHost());
		}
		connection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:26.0) Gecko/20100101 Firefox/26.0");
		if (httPParameter.getConnection() != null) {
			connection.addRequestProperty("Connection", httPParameter.getConnection());
		}
		
		if ("POST".equals(httPParameter.getPisG())) {// 判断是get还是post
			
			connection.getOutputStream().write(httPParameter.getParameter().getBytes());
			connection.connect();// 提交
		} else {
			connection.setRequestMethod("GET");// 设置提交方式
			connection.connect();// 提交
		}
		
		
		if("IMG".equals(httPParameter.getRetCodeType())){
			  //取图片数据
				if(connection.getInputStream()!=null){
					DataInputStream in = new DataInputStream(connection.getInputStream());
					DataOutputStream out = new DataOutputStream(new FileOutputStream("d://验证码.jpg"));
					//将参数savePath,即将截取的图片的存储在本地地址赋
					byte[] buffer = new byte[1024];
					int count = 0;
					//将输入流以字节的形式读取并写入buffer
					while ((count = in.read(buffer)) > 0) {
						out.write(buffer, 0, count);
					}
					// 关闭输入输出流
					out.close();
					in.close();
				}
			}
			if("TXT".equals(httPParameter.getRetCodeType())){
				// 取数据
				if(connection.getInputStream()!=null){
				InputStream ins = connection.getInputStream();
				if (ins!=null) {
					// 读取返回的信息
					BufferedReader in = new BufferedReader(new InputStreamReader(ins, "UTF-8"));
					StringBuffer line =new StringBuffer();
					String tem = "";
					while ((tem = in.readLine()) != null) {
						line.append(tem);
					}
					in.close();
					cookie.setCode(line.toString());
				}
				ins.close();
				
				}
			}
			
			// 取cookie
			if (connection.getHeaderField("Set-Cookie") != null) {
				String cookie1 = "";
				for (String s : connection.getHeaderFields().get("Set-Cookie")) {
					cookie1 += s;
				}
				cookie.setCookie(cookie1);
			}
			//重定向地址
			if (connection.getHeaderField("Location") != null) {
				String Location = "";
				for (String s : connection.getHeaderFields().get("Location")) {
					Location += s;
				}
				cookie.setLocation(Location);
			}
			
			connection.disconnect();// 关闭
			return cookie;
		
	}
写了个模拟登陆 oschina的java代码,我现在发带图片的动弹,写不出来了。我模拟不来post提交的图片地址文件,求大神帮帮我。谢谢! 写模拟登陆不是为了发垃圾动弹,只做技术研究!谢谢

@开源中国首席骨科主任   @红薯  @铂金小鸟

/**
	 * HTTP提交
	 * @throws IOException 
	 */
	public static Cookie getHTTPPost(HTTPParameter httPParameter) throws Exception{
		Cookie cookie=new Cookie();
		URL url2 = new URL(httPParameter.getUrl());
		HttpURLConnection connection = (HttpURLConnection) url2.openConnection();
		connection.setDoOutput(true);// 打开写入属性
		connection.setDoInput(true);// 打开读取属性
		connection.setReadTimeout(3000);
		connection.setConnectTimeout(10000);// 连接超时时间 设置连接主机超时(单位:毫秒)
		connection.setReadTimeout(10000);// 设置从主机读取数据超时(单位:毫秒
		connection.setInstanceFollowRedirects(false);// 设置是否重定向
		if(httPParameter.getReferer()!=null){
			connection.addRequestProperty("Referer", httPParameter.getReferer());
		}
		
		if(httPParameter.getCookie()!=null){
			connection.addRequestProperty("Cookie", httPParameter.getCookie());
		}
		if(httPParameter.getHost()!=null){
			connection.addRequestProperty("Host", httPParameter.getHost());
		}
		connection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:26.0) Gecko/20100101 Firefox/26.0");
		if (httPParameter.getConnection() != null) {
			connection.addRequestProperty("Connection", httPParameter.getConnection());
		}
		
		if ("POST".equals(httPParameter.getPisG())) {// 判断是get还是post
			
			connection.getOutputStream().write(httPParameter.getParameter().getBytes());
			connection.connect();// 提交
		} else {
			connection.setRequestMethod("GET");// 设置提交方式
			connection.connect();// 提交
		}
		
		
		if("IMG".equals(httPParameter.getRetCodeType())){
			  //取图片数据
				if(connection.getInputStream()!=null){
					DataInputStream in = new DataInputStream(connection.getInputStream());
					DataOutputStream out = new DataOutputStream(new FileOutputStream("d://验证码.jpg"));
					//将参数savePath,即将截取的图片的存储在本地地址赋
					byte[] buffer = new byte[1024];
					int count = 0;
					//将输入流以字节的形式读取并写入buffer
					while ((count = in.read(buffer)) > 0) {
						out.write(buffer, 0, count);
					}
					// 关闭输入输出流
					out.close();
					in.close();
				}
			}
			if("TXT".equals(httPParameter.getRetCodeType())){
				// 取数据
				if(connection.getInputStream()!=null){
				InputStream ins = connection.getInputStream();
				if (ins!=null) {
					// 读取返回的信息
					BufferedReader in = new BufferedReader(new InputStreamReader(ins, "UTF-8"));
					StringBuffer line =new StringBuffer();
					String tem = "";
					while ((tem = in.readLine()) != null) {
						line.append(tem);
					}
					in.close();
					cookie.setCode(line.toString());
				}
				ins.close();
				
				}
			}
			
			// 取cookie
			if (connection.getHeaderField("Set-Cookie") != null) {
				String cookie1 = "";
				for (String s : connection.getHeaderFields().get("Set-Cookie")) {
					cookie1 += s;
				}
				cookie.setCookie(cookie1);
			}
			//重定向地址
			if (connection.getHeaderField("Location") != null) {
				String Location = "";
				for (String s : connection.getHeaderFields().get("Location")) {
					Location += s;
				}
				cookie.setLocation(Location);
			}
			
			connection.disconnect();// 关闭
			return cookie;
		
	}

展开
收起
kun坤 2020-06-09 15:29:04 686 0
1 条回答
写回答
取消 提交回答
  • 用openapi吧 http://www.oschina.net/openapi/docs/tweet_pub######大神,如果用openapi, 模拟提交文件的技术我就没学到了,就没用了,请谅解小的对技术的执着###### 真的还不如试下HttpClient 这个工具包省事很多。。
    提交文件模拟一大段字符串,你若HttpConnect的话,可以尝试自己写个简单的文件提交页面,然后把Http Body那段的格式抄一下,查点资料对比下试下就好了 ######不行,还有你说的httpclient什么包的,?不是jdk自带的?###### http://my.oschina.net/action/tweet/insert_img?user=1456141
    用这个地址去上传吧。分析一下OSC是用什么上传控件的。
    ######是的啊- -但是不会上传啊!!妈蛋###### http://yonge812.iteye.com/blog/1477680
     一查一大堆啊 ######http://314858770.iteye.com/blog/720456######我收到了惊吓

    2020-06-10 09:36:38
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载