C# 调用 HTTPWebRequest 实现简单Get请求

 

 

学了两天C#写了个快递查询,没有做Json解析。

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.IO;
namespace WindowsFormsApp4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show ((Get(textBox1.Text)));
}
private string Get(string str)
{
HttpWebRequest macro = HttpWebRequest.Create(“http://www.kuaidi100.com/query?type=yunda&postid=“ + textBox1.Text) as HttpWebRequest;
macro.Method = “GET”;
macro.UserAgent = “Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36”;
HttpWebResponse response = (HttpWebResponse)macro.GetResponse();
string res = new System.IO.StreamReader(response.GetResponseStream(), Encoding.GetEncoding(“utf-8”)).ReadToEnd();
return res;
}
}