提交用户名和密码到指定的服务器
来源:互联网 发布:大非农数据利空金银 编辑:程序博客网 时间:2024/06/10 01:51
package com.phone.week6.day3;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class TestDownLoadAndlisten {
public static void main(String[] args) { //一边下载保存在本地,一边下载显示在控件中 //两个线程来做,主线程在下载显示在控件中,子线程在下载并且保存 try { final URL url = new URL("http://localhost:8080/web1509/dog.jpg"); //这个子线程就是用来下载保存在本地的 new Thread(new Runnable() { @Override public void run() { //调用下载保存本地的方法 DownLoadUtil.saveImage(url, new Tip() { @Override public void showOk(byte[] b) { String str = new String(b); System.out.println(str); } }); } }).start(); //主线程边下载边显示 DownLoadUtil.showImage(url, new Tip() { @Override public void showOk(byte[] b) { //当这个循环在模拟往控件中显示 for (int i = 0; i <b.length; i++) { //System.out.println(b[i]); } } }); } catch (MalformedURLException e) { e.printStackTrace(); }}
}
//下载的工具类
class DownLoadUtil{
//下载保存的方法public static void saveImage(URL url,Tip tip){try { //1.建立程序与资源之间的连接 HttpURLConnection conn =(HttpURLConnection) url.openConnection(); //2.设置请求的方式 conn.setRequestMethod("GET"); //3.设置操作流的方式 conn.setDoInput(true); //4.获得服务器上的响应码 int code = conn.getResponseCode(); //5.根据响应码执行操作 if(code==conn.HTTP_OK){ //先要获得输入流 InputStream is = conn.getInputStream(); //创建一个文件输出流 FileOutputStream fos = new FileOutputStream("d:\\cat.jpg"); int len = 0; while((len=is.read())!=-1){ fos.write(len); fos.flush(); System.out.println("保存-----"+len); } //释放资源 fos.close(); is.close(); conn.disconnect(); //服务器端给响应 tip.showOk("恭喜您下载成功".getBytes()); }} catch (IOException e) { e.printStackTrace();}}//边下载边显示public static void showImage(URL url,Tip tip){ try { //1.建立程序与资源之间的连接 HttpURLConnection conn =(HttpURLConnection) url.openConnection(); //2.设置请求的方式 conn.setRequestMethod("GET"); //3.设置操作流的方式 conn.setDoInput(true); //4.获得服务器上的响应码 int code = conn.getResponseCode(); //5.根据响应码执行操作 if(code==conn.HTTP_OK){ //先要获得输入流 InputStream is = conn.getInputStream(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); int len = 0; while((len=is.read())!=-1){ bos.write(len); System.out.println("下载********"+len); } //释放资源 is.close(); conn.disconnect(); //服务器端给响应 tip.showOk(bos.toByteArray()); } } catch (IOException e) { e.printStackTrace(); }}
}
//下载成功后,希望服务器端给的响应内容,由于第个客户端需要得到的响应内容不一样,所以我们采用接口回调的方式
interface Tip{
void showOk(byte[] b); //响应的结果的方法
}
package com.phone.week6.day3;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
public class TestHttpClient {
public static void main(String[] args) { String name = "wer"; String pwd = "20"; //以POST方式请求 try { //1.创建一个HttpClient对象 HttpClient client = new DefaultHttpClient(); //2.创建请求的对象 HttpPost post = new HttpPost("http://localhost:8080/web1509/LoginServlet"); //如果是以POST方式请求,你一定要设置请求体 List<NameValuePair> list = new ArrayList<>(); list.add(new BasicNameValuePair("uname", name)); list.add(new BasicNameValuePair("pwd", pwd)); post.setEntity(new UrlEncodedFormEntity(list, "gbk")); //3.发送请求 HttpResponse resp=client.execute(post); //4.得到响应的状态码 int code = resp.getStatusLine().getStatusCode(); //5.根据状态码判断 if(code==200){ HttpEntity entity =resp.getEntity(); String reply =EntityUtils.toString(entity, "GBK"); System.out.println(reply); } client.getConnectionManager().shutdown(); //关闭资源 } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }}private static void test1() { //下载服务器端的图片 //1.先创建一个浏览器对象 //HttpClient是个接口,所以不能直接new,要new它最常用的实现类DefaultHttpClient HttpClient client = new DefaultHttpClient(); //2.创建一种请求方式,以get方式请求 HttpGet get = new HttpGet("http://localhost:8080/web1509/dog.jpg"); try { //3.发送请求 HttpResponse res=client.execute(get); //4.获得状态码 int code=res.getStatusLine().getStatusCode(); //5.根据状态码判断 if(code==200){ //获得输入流,通过调用响应对象的getEntity()方法会返回一个HttpEntity对象,这个对象里封装了所有的响应信息 //调用HttpEntity对象的getContent()方法会返回所有内容的信息并封装在输入流里 InputStream is = res.getEntity().getContent(); FileOutputStream fos = new FileOutputStream("d:\\person.jpg"); int len=0; while((len=is.read())!=-1){ fos.write(len); fos.flush(); } //释放资源 fos.close(); is.close(); client.getConnectionManager().shutdown(); //关闭连接 System.out.println("下载成功"); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }}
}
package com.phone.week6.day3;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;
public class TestUrlHttpConnection {
public static void main(String[] args) {
//我提交用户名和密码到指定的服务器,然后服务器能给我一定的响应
//http://localhost:8080/web1509/LoginServlet
Scanner in = new Scanner(System.in);
System.out.println(“请输入用户名”);
String name = in.next();
System.out.println(“请输入密码”);
String pwd = in.next();
try {
//1.根据指定的地址封装成一个URL对象
URL url = new URL(“http://localhost:8080/web1509/LoginServlet?uname=“+name+”&pwd=”+pwd);
//2.建立程序与资源之间连接并打开连接
HttpURLConnection conn= (HttpURLConnection) url.openConnection();
//3.设置请求资源的方式
conn.setRequestMethod(“GET”);
//4.设置操作流的方式,是读还是写
conn.setDoOutput(true); //表示是往服务器端写
//6.获得服务器的响应码
int code = conn.getResponseCode();
//7.根据响应码判断
if(code==200){
//8.获得服务器端的响应
InputStream is = conn.getInputStream(); //得到输入流
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String reply = br.readLine(); //读到服务器端的响应
System.out.println(“服务器端说:”+reply);
//9.释放资源 br.close(); conn.disconnect(); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }}private static void test2() { //我提交用户名和密码到指定的服务器,然后服务器能给我一定的响应 //http://localhost:8080/web1509/LoginServlet Scanner in = new Scanner(System.in); System.out.println("请输入用户名"); String name = in.next(); System.out.println("请输入密码"); String pwd = in.next(); try { //1.根据指定的地址封装成一个URL对象 URL url = new URL("http://localhost:8080/web1509/LoginServlet"); //2.建立程序与资源之间连接并打开连接 HttpURLConnection conn= (HttpURLConnection) url.openConnection(); //3.设置请求资源的方式 conn.setRequestMethod("POST"); //构造一个参数字符串 String param ="uname="+name+"&pwd="+pwd; //4.设置操作流的方式,是读还是写 conn.setDoOutput(true); //表示是往服务器端写 //5.通过连接的对象获得一个输出流 OutputStream os=conn.getOutputStream(); os.write(param.getBytes()); //把请求体写到服务器端去 //注意:在往服务器端写内容的时候,尤其是以POST方式提交,一定要先写,然后再获得服务器端的响应码才行 //POST方式请求服务器传值是以流的方式来传数据的,所以一定要先设置conn.setDoOutput(true); //6.获得服务器的响应码 int code = conn.getResponseCode(); System.out.println(code); //7.根据响应码判断 if(code==200){ //8.获得服务器端的响应 InputStream is = conn.getInputStream(); //得到输入流 BufferedReader br = new BufferedReader(new InputStreamReader(is)); String reply = br.readLine(); //读到服务器端的响应 System.out.println("服务器端说:"+reply); //9.释放资源 br.close(); os.close(); conn.disconnect(); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }}private static void test1() { //想从指定服务器上下载一个指定的资源 //http://10.2.152.55:8080/web1509/dog.jpg; try { //1.根据指定的地址封装成一个URL对象 URL url = new URL("http://localhost:8080/web1509/dog.jpg"); //2.建立程序与资源之间的连接 HttpURLConnection conn =(HttpURLConnection)url.openConnection(); //3.设置程序请求资源的方式,GET,POST conn.setRequestMethod("GET"); //参数一定要大写 //4.获得服务器端的响应状态码 int code = conn.getResponseCode(); //5.根据状态码来判断结果,要不要下载资源 //如果状态码是200,表示响应OK if(code==conn.HTTP_OK){ //准备下载资源 //6.得到输入流 InputStream is=conn.getInputStream(); //创建一个文件输出流 FileOutputStream fos = new FileOutputStream(new File("d:\\dog.jpg")); int len = 0; while((len=is.read())!=-1){ fos.write(len); fos.flush(); } //7.释放资源 fos.close(); is.close(); conn.disconnect(); //关掉连接 System.out.println("下载成功"); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }}
}
- 提交用户名和密码到指定的服务器
- “用指定的用户名和密码无法登录到该ftp服务器”解决办法
- 一段提交用户名和密码到指定网址的代码(大漠提供)--青蛙非王子(lostfrog)
- 连接到localhost 提示:位于 XDB 的服务器 localhost 要求用户名和密码
- 连接到localhost 提示:位于 XDB 的服务器 localhost 要求用户名和密码
- Tomcat连接到localhost 提示:位于 XDB 的服务器 localhost 要求用户名和密码
- 连接到localhost 提示:位于 XDB 的服务器 localhost 要求用户名和密码
- Git 每次提交时的用户名和密码设置
- Java传入用户名和密码并自动提交表单实现登录到其他系统的实例代码
- Git提交记住用户名和密码
- NOD32 升级无需用户名和密码的服务器列表
- 位于 XDB 的服务器 localhost 要求用户名和密码!!
- 关于访问服务器需要输入用户名和密码的解决办法
- 位于XDB的服务器localhost要求用户名和密码
- 位于XDB的服务器localhost要求用户名和密码
- 位于XDB的服务器localhost要求用户名和密码
- 位于 XDB 的服务器 localhost 要求用户名和密码!!
- 位于XDB的服务器127.0.0.1要求用户名和密码
- 0907Android数据存储
- javascript 构造函数内return 解析
- 制作word生成器感悟
- 第2周项目1 函数参数传递的三种方式
- POJ3650解题报告
- 提交用户名和密码到指定的服务器
- [LeedCode OJ]#19 Remove Nth Node From End of List
- Mms 短信设置里添加常用短信时出现错误的解决办法
- 06-查看dns
- 机房收费——“上下机”还原生活
- netfilter/hook关于ICMP协议过滤的样例
- ListView+RecyclerView
- Java面试题
- sublime 常用正则表达式