当前位置: 首页 > news >正文

海南省建设厅网站首页自己做淘宝客登录网站

海南省建设厅网站首页,自己做淘宝客登录网站,江苏城乡建设,wordpress主题 德国转自#xff1a; java网络编程-HTTP编程_Stillsings的博客-CSDN博客HTTP编程Java HTTP编程支持模拟成浏览器的方式去访问网页URL, Uniform Resource Locator#xff0c;代表一个资源URLConnection获取资源连接器根据URL的openConnection#xff08;#xff09;方法获得URL…转自 java网络编程-HTTP编程_Stillsings的博客-CSDN博客HTTP编程Java HTTP编程支持模拟成浏览器的方式去访问网页URL, Uniform Resource Locator代表一个资源URLConnection获取资源连接器根据URL的openConnection方法获得URLConnectionconnect方法建立和资源的联系通道getInputStream方法获取资源的内容示例代码Get获取网页h...https://blog.csdn.net/Listen_heart/article/details/104448012 【1】HTTP编程 Java HTTP编程 支持模拟成浏览器的方式去访问网页     URL, Uniform Resource Locator代表一个资源     URLConnection         获取资源连接器         根据URL的openConnection方法获得URLConnection         connect方法建立和资源的联系通道         getInputStream方法获取资源的内容 【2】URLConnection 示例代码1Get获取网页html-使用URLConnection package com.lihuan.network.demo03;import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.nio.charset.StandardCharsets; import java.util.List; import java.util.Map;public class URLConnectionGetTest {public static void main(String[] args) {try {String urlName http://www.baidu.com;URL url new URL(urlName);URLConnection connection url.openConnection();//建立联系通道connection.connect();//打印http的头部信息MapString, ListString headers connection.getHeaderFields();for (Map.EntryString, ListString entry : headers.entrySet()){String key entry.getKey();for (String value : entry.getValue()){System.out.println(key : value);}}//输出将要收到的内容属性信息System.out.println(-------------);System.out.println(getContentType: connection.getContentType());System.out.println(getContentLength: connection.getContentLength());System.out.println(getContentEncoding: connection.getContentEncoding());System.out.println(getDate: connection.getDate());System.out.println(getExpiration: connection.getExpiration());System.out.println(getLastModified: connection.getLastModified());System.out.println(-------------);BufferedReader br new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));// 输出收到的内容String line ;while ((line br.readLine()) ! null){System.out.println(line);}br.close();} catch (IOException e) {e.printStackTrace();}} } 实例代码2 Post提交表单-使用 HttpURLConnection package com.lihuan.network.demo03;import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.net.*; import java.util.HashMap; import java.util.Map; import java.util.Scanner;public class URLConnectionPostTest {public static void main(String[] args) throws IOException {String urlString https://tools.usps.com/zip-code-lookup.htm?byaddress;Object userAgent HTTPie/0.9.2;Object redirects 1;CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));MapString, String params new HashMapString, String();params.put(tAddress, 1 Market Street);params.put(tCity, san Francisco);params.put(sState, CA);String result doPost(new URL(urlString), params,userAgent null ? null : userAgent.toString(),redirects null ? -1 : Integer.parseInt(redirects.toString()));System.out.println(result);}public static String doPost(URL url, MapString, String nameValuePairs, String userAgent, int redirects) throws IOException {HttpURLConnection connection (HttpURLConnection) url.openConnection();//设置请求头if(userAgent ! null){connection.setRequestProperty(User-Agent, userAgent);}//设为不自动重定向if(redirects 0){connection.setInstanceFollowRedirects(false);}//设置可以使用conn.getOutputStream().printconnection.setDoOutput(true);//输出请求的参数try (PrintWriter out new PrintWriter(connection.getOutputStream())){boolean first true;for (Map.EntryString, String pair : nameValuePairs.entrySet()){//参数拼接if(first){first false;}else{out.print();}String name pair.getKey();String value pair.getValue();out.print(name);out.print();out.print(URLEncoder.encode(value, UTF-8));}}String encoding connection.getContentEncoding();if(encoding null){encoding UTF-8;}if(redirects 0){int responseCode connection.getResponseCode();System.out.println(responseCode: responseCode);if(responseCode HttpURLConnection.HTTP_MOVED_PERM|| responseCode HttpURLConnection.HTTP_MOVED_TEMP|| responseCode HttpURLConnection.HTTP_SEE_OTHER){String location connection.getHeaderField(Location);if(location ! null){URL base connection.getURL();connection.disconnect();return doPost(new URL(base, location), nameValuePairs, userAgent, redirects - 1);}}}else if(redirects 0){throw new IOException(Too many redirects);}//接下来获取html内容StringBuilder response new StringBuilder();try (Scanner in new Scanner(connection.getInputStream(), encoding)){while (in.hasNextLine()){response.append(in.nextLine());response.append(\n);}}catch (IOException e){InputStream err connection.getErrorStream();if(err null) throw e;try (Scanner in new Scanner(err)){response.append(in.nextLine());response.append(\n);}}return response.toString();} } 【3】JDK HttpClient JDK 9新增JDK10更新JDK11正式发     java.net.http包     取代URLConnection     支持HTTP/1.1和HTTP/2     实现大部分HTTP方法     主要类         HttpClient         HttpRequest         HttpResponse HttpComponents 是一个集成的Java HTTP工具包     实现所有HTTP方法: get/post/put/delete     支持自动转向     支持https协议     支持代理服务器等 HttpComponent示例代码3Get获取网页html package com.lihuan.network.demo04;import org.apache.http.HttpResponse; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils;import java.io.IOException;public class HttpComponentGetTest {public static void main(String[] args) {CloseableHttpClient httpClient HttpClients.createDefault();RequestConfig requestConfig RequestConfig.custom().setConnectTimeout(5000).setConnectionRequestTimeout(5000).setSocketTimeout(5000).setRedirectsEnabled(true).build();HttpGet httpGet new HttpGet(http://www.baidu.com);httpGet.setConfig(requestConfig);String strResult ;try {HttpResponse httpResponse httpClient.execute(httpGet);if(httpResponse.getStatusLine().getStatusCode() 200){strResult EntityUtils.toString(httpResponse.getEntity(), UTF-8);System.out.println(strResult);}else{}} catch (IOException e) {e.printStackTrace();}finally {try {httpClient.close();} catch (IOException e) {e.printStackTrace();}}} } HttpComponent实例代码4Post提交表单 package com.lihuan.network.demo04;import org.apache.http.HttpResponse; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.LaxRedirectStrategy; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils;import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.ArrayList; import java.util.List;public class HttpComponentsPostTest {public static void main(String[] args) throws UnsupportedEncodingException {//获取可关闭的 httpClientCloseableHttpClient httpClient HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy()).build();//配置超时时间RequestConfig requestConfig RequestConfig.custom().setConnectTimeout(10000).setConnectionRequestTimeout(10000).setSocketTimeout(10000).setRedirectsEnabled(false).build();HttpPost httpPost new HttpPost(https://tools.usps.com/zip-code-lookup.htm?byaddress);//设置超时时间httpPost.setConfig(requestConfig);ListBasicNameValuePair list new ArrayList();list.add(new BasicNameValuePair(tAddress, URLEncoder.encode(1 Market Street, UTF-8)));list.add(new BasicNameValuePair(tCity, URLEncoder.encode(san Francisco, UTF-8)));list.add(new BasicNameValuePair(sState, CA));try {UrlEncodedFormEntity entity new UrlEncodedFormEntity(list, UTF-8);//设置post请求参数httpPost.setEntity(entity);httpPost.setHeader(User-Agent, HTTPie/0.9.2);HttpResponse httpResponse httpClient.execute(httpPost);String strResult ;if(httpResponse ! null){System.out.println(httpResponse.getStatusLine().getStatusCode());if(httpResponse.getStatusLine().getStatusCode() 200){strResult EntityUtils.toString(httpResponse.getEntity());}else{strResult Error Response httpResponse.getStatusLine().toString();}}else{}System.out.println(strResult);} catch (IOException e) {e.printStackTrace();} finally {if(httpClient ! null){try {httpClient.close();} catch (IOException e) {e.printStackTrace();}}}} }
http://www.yutouwan.com/news/461570/

相关文章:

  • 什么 电子商务网站建设与管网站相互推广怎么做
  • 静态网站登陆怎么做网站建设购买什么境外主机
  • 江苏省内网站建设移动商城个人中心
  • 网站做标签搭建一个网站多少钱
  • 网站源码做exe执行程序网络营销如何进行网站推广
  • 长沙做网站推广公司咨询犀牛云网站做的怎么样
  • 手机网站开发标准即时设计网页
  • 如何创建一个网站用来存放东西合肥网站推广 公司
  • 免费的开发网站建设哪里 教做网站带维护
  • 公众号怎么做微网站html 购物网站
  • 怎么做查询网站网站前台和后台
  • WordPress站群模版开发一个小程序流程
  • 建设网站合同范本登不了wordpress
  • 外贸网站示例哪里有好网站设计
  • 河间哪里有做网站的上海网站建设导航
  • 网站搭建的意义个人网站备案没有座机
  • 资中移动网站建设平台引流推广怎么做
  • 建设信用卡在网站挂失几步58同城最新招聘网
  • 宝和网站建设如何建设酒店预订系统网站
  • 会计题库网站怎么做win7如何安装iis来浏览asp网站
  • 免费自己制作app软件下载安徽优化网站
  • 济南住房与城乡建设官网网站标题具体怎样优化
  • 学校网站下载租用云服务器多少钱
  • 慈溪市网站开发如何选择宜昌网站建设
  • 做视频网站需要什么服务器配置建设网站初步目标咋写
  • 家居网站建设营销推广来个网站2021能用的
  • 名气特别高的手表网站东源建设局网站
  • 建设局特种作业网站湖北省建设厅网站首页
  • 东莞网站建设品牌网站开发兼容问题
  • 一级造价工程师报名网站数码产品商务网站建设