WPF 初学 - Path

使用HttpClient进行网络处理的基本步骤如下:

1、通过get的方式获取到Response对象。

CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet("http://www.baidu.com/");
CloseableHttpResponse response = httpClient.execute(httpGet);

2、获取Response对象的Entity。

HttpEntity entity = response.getEntity();

注:HttpClient将Response的正文及Request的POST/PUT方法中的正文均封装成一个HttpEntity对象。可以通过entity.getContenType(),entity.getContentLength()等方法获取到正文的相关信息。但最重要的方法是通过getContent()获取到InputStream对象。

3、通过Entity获取到InputStream对象,然后对返回内容进行处理。

is = entity.getContent();
sc = new Scanner(is);
// String filename = path.substring(path.lastIndexOf(‘/‘)+1);
String filename = "2.txt";
os = new PrintWriter(filename);
while (sc.hasNext()) {
	os.write(sc.nextLine());
}

使用HtppClient下载一个网页的完整代码如下:

package com.ljh.test;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.Scanner;

import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

public class DownloadWebPage{

	public static void downloadPagebyGetMethod() throws IOException {

		// 1、通过HttpGet获取到response对象
		CloseableHttpClient httpClient = HttpClients.createDefault();
		HttpGet httpGet = new HttpGet("http://www.baidu.com/");
		CloseableHttpResponse response = httpClient.execute(httpGet);

		InputStream is = null;
		Scanner sc = null;
		Writer os = null;
		if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
			try {
				// 2、获取response的entity。
				HttpEntity entity = response.getEntity();

				// 3、获取到InputStream对象,并对内容进行处理
				is = entity.getContent();
				sc = new Scanner(is);
				// String filename = path.substring(path.lastIndexOf(‘/‘)+1);
				String filename = "2.txt";
				os = new PrintWriter(filename);
				while (sc.hasNext()) {
					os.write(sc.nextLine());
				}

			} catch (ClientProtocolException e) {
				e.printStackTrace();
			} finally {
				if (sc != null) {
					sc.close();
				}
				if (is != null) {
					is.close();
				}
				if (os != null) {
					os.close();
				}
				if (response != null) {
					response.close();
				}
			}
		}

	}

	public static void main(String[] args) {
		try {
			downloadPagebyGetMethod();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

注意:直接将HttpGet改为HttpPost,返回的结果有误,百度返回302状态,即重定向,新浪返回拒绝访问。怀疑大多网站均不允许POST方法直接访问网站。

WPF 初学 - Path,布布扣,bubuko.com

时间: 2024-08-03 20:44:50

WPF 初学 - Path的相关文章

WPF初学(一)——布局【良好界面的基础】

由Winform转到WPF的一部分人,很可能忽略掉布局,习惯性的使用固定定位.然而,没有良好的布局,后面界面控件画的再好看,花哨,都不过是鲜花插在牛粪上,很可能始终都是一坨??(呵呵). 闲话少说,首先说说WPF中最常用,其实功能也最强大的布局--Grid,很多时候,偶们只会注意到其最常用这一特点,而其之强大往往被忽略,其强大之处在于他可以替换到其他大部分布局面板,当然使用起来或许没有那些特种需求布局面板方便. 简例:Grid布局3行3列,行等分,列比例为1:2:3 <Grid> <Gr

WPF使用Path画圆的一个示例代码

原文:WPF使用Path画圆的一个示例代码 <Path Fill="red" Data="M 0,0 A 20,20 45 1 1 0,1 Z"/> 1. Fill是指Path的填充颜色 2. Data是Path的路径, M 0,0  --画笔移到0,0处: A 20,20 --画半径为20的圆弧: 45 1 1 --- 45为转角角度,1:是否有大小弧,画椭圆时才有用:1:顺时针 0,1 --- 终点 Z --- 闭合 原文地址:https://www

WPF初学之The Current Application instance

这篇主要讲Application.Current属性.在WPF中,我们可以在应用程序域中的任何地方通过Application.Current.MainWindow来获取访问主程序.如何你想获取主程序中的方法,属性,事件等,你就需要把mainwindow转成你的MainWindow窗体. Application.Current.Windows能获取当前打开的window窗口. 1 Window main = Application.Current.MainWindow; 2 MessageBox.

WPF初学之绘制自己需要的Slider

最近研究了slider的一些功能.主要是修改他们的样式,换成我们想要的控件模型.进度度显示等.首先说一下slider的组成 Slider 控件允许用户选择值从值的范围. 下面的插图演示了 Slider 控件的一个示例. slider 控件的示例 可以通过设置 Slider 控件的属性来自定义该控件. 下表描述可以自定义的 Slider 的一些属性: Slider 的方向,水平或垂直. 沿 Slider 跟踪的刻度线位置. 显示 Slider的当前值的工具提示显示. Thumb 的能力.对齐的滴答

WPF初学之绘制自己需要的Slider(之二)

结合之前的样式,做了一下修改和美化,贴出来!!! 1 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 2 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 3 4 <!--Control colors.--> 5 <Color x:Key="D

WPF初学之LinearGradientBrush

LinearGradientBrush 使用线性渐变绘制区域. 线性渐变沿直线定义渐变. 该直线的终点由线性渐变的 StartPoint 和 EndPoint 属性定义.LinearGradientBrush 画笔沿此直线绘制其 GradientStops. 默认的线性渐变是沿对角方向进行的. 默认情况下,线性渐变的 StartPoint 是被绘制区域的左上角 (0,0),其 EndPoint 是被绘制区域的右下角 (1,1). 所得渐变的颜色是沿着对角方向路径插入的. 下图演示对角渐变. 其中

Photoshop和WPF双剑配合,打造炫酷个性的进度条控件

现在如果想打造一款专业的App,UI的设计和操作的简便性相当重要.UI设计可以借助Photoshop或者AI等设计工具,之前了解到WPF设计工具Expression Blend可以直接导入PSD文件或者AI设计文件(当然不是全部特征支持),最近研究了一下,也废了一番周折,好在最后实现了预期的效果.下面将step by step用示例说明如何先用PS构建一个矢量图形模板,然后用Expression Blend导入PSD文件,并获取PATH的Data值,为打造一款炫酷的个性进度条控件构建美观UI.

WPF中的3D Wireframe

原文:WPF中的3D Wireframe WPF不支持画三维线,但开发人员提供了ScreenSpaceLines3D 类用于实现这个功能.我已经在程序中实现并成功显示3D Wireframe,并能够进行3D Solid和3D Wireframe的切换. 我在熟悉这个类的基础上,自己定义了3D Wireframe xml文件的格式,用于保存3D Wireframe数据. 格式如下: <Wireframe>? <ScreenSpaceLines3D??? Form="Generic

WPF datagrid 初学

<Window x:Class="WpfDemo.WinDataGrid" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:assembly="clr-namespace:System;assembly=mscorlib&quo