webservice 接口通过 HTTP 获取数据

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Text;
using System.IO; namespace testRsa
{
public class GetDataByHttp
{ public static string DoPost(string url, string data)
{
HttpWebRequest req = GetWebRequest(url, "POST");
byte[] postData = Encoding.UTF8.GetBytes(data);
Stream reqStream = req.GetRequestStream();
reqStream.Write(postData, , postData.Length);
reqStream.Close();
HttpWebResponse rsp = (HttpWebResponse)req.GetResponse();
Encoding encoding = Encoding.GetEncoding(rsp.CharacterSet);
return GetResponseAsString(rsp, encoding);
} public static HttpWebRequest GetWebRequest(string url, string method)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.ServicePoint.Expect100Continue = false;
req.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
req.ContentType = "text/json";
req.Method = method;
req.KeepAlive = true;
req.UserAgent = "guanyisoft";
req.Timeout = ;
req.Proxy = null;
return req;
} public static string GetResponseAsString(HttpWebResponse rsp, Encoding encoding)
{
StringBuilder result = new StringBuilder();
Stream stream = null;
StreamReader reader = null;
try
{
// 以字符流的方式读取HTTP响应
stream = rsp.GetResponseStream();
reader = new StreamReader(stream, encoding);
// 每次读取不大于256个字符,并写入字符串
char[] buffer = new char[];
int readBytes = ;
while ((readBytes = reader.Read(buffer, , buffer.Length)) > )
{
result.Append(buffer, , readBytes);
}
}
finally
{
// 释放资源
if (reader != null) reader.Close();
if (stream != null) stream.Close();
if (rsp != null) rsp.Close();
} return result.ToString();
} }
}
  
上一篇:INNO 补丁制作技术, 打开 INNO 补丁制作方法的第一页


下一篇:用Python写了个抓图小脚本