首页 > 代码库 > winform访问url传参有返回值
winform访问url传参有返回值
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://。。。。");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string ret = string.Empty;
Stream s;
string StrDate = "";
string strValue = "";
if (response.StatusCode == HttpStatusCode.OK)
{
s = response.GetResponseStream();
////在这儿处理返回的文本就OK了,如:
StreamReader Reader = new StreamReader(s, Encoding.UTF8);
while ((StrDate = Reader.ReadLine()) != null)
{
strValue += StrDate + "\r\n";
}
}
Console.WriteLine(strValue);
Console.ReadKey();
}
/*
Winform传递参数至Url并获得返回值
在winform里面调用URL并传递参数主要使用HttpWebRequest对象,分为两种形式post和get,个人感觉就是模拟了浏览器,
1.post方法:
/// <summary>
/// 使用post方法,调用短信接口
/// </summary>
/// <param name="PhoneNumber"></param>
/// <param name="SmsContent"></param>
/// <param name="PhoneNumberType"></param>
/// <param name="SmsUser"></param>
private void CallMsgCenterToSendMsgPost(string PhoneNumber, string SmsContent, string PhoneNumberType, string SmsUser)
{
string formUrl = ConfigurationSettings.AppSettings["formUrl"].ToString().Trim();//url地址
string formData = "http://www.mamicode.com/PhoneNumber=" + PhoneNumber + "&SmsContent=" +SmsContent + "&PhoneNumberType=" + PhoneNumberType + "&SmsUser=" + SmsUser + "";
CookieContainer cookieContainer = new CookieContainer();
// 将提交的字符串数据转换成字节数组
byte[] postData = http://www.mamicode.com/Encoding.UTF8.GetBytes(formData);
// 设置提交的相关参数
HttpWebRequest request = WebRequest.Create(formUrl) as HttpWebRequest;
Encoding myEncoding = Encoding.GetEncoding("gb2312");
request.Method = "POST";
request.KeepAlive = false;
request.AllowAutoRedirect = true;
request.ContentType = "application/x-www-form-urlencoded";
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
request.CookieContainer = cookieContainer;
request.ContentLength = postData.Length;
// 提交请求数据
System.IO.Stream outputStream = request.GetRequestStream();
outputStream.Write(postData, 0, postData.Length);
outputStream.Close();
HttpWebResponse response;
Stream responseStream;
StreamReader reader;
string srcString;
response = request.GetResponse() as HttpWebResponse;
responseStream = response.GetResponseStream();
reader = new System.IO.StreamReader(responseStream, Encoding.UTF8);
srcString = reader.ReadToEnd();
reader.Close();
}
2,get方法
/// <summary>
/// 使用get方法
/// </summary>
/// <param name="PhoneNumber"></param>
/// <param name="SmsContent"></param>
/// <param name="PhoneNumberType"></param>
/// <param name="SmsUser"></param>
private void CallMsgCenterToSendMsgGet(string PhoneNumber, string SmsContent, string PhoneNumberType, string SmsUser)
{
string formUrl = ConfigurationSettings.AppSettings["formUrl"].ToString().Trim();
string formData = "http://www.mamicode.com/PhoneNumber=" + HttpUtility.UrlEncode(PhoneNumber) + "&SmsContent=" + HttpUtility.UrlEncode(SmsContent) + "&PhoneNumberType=" + HttpUtility.UrlEncode(PhoneNumberType) + "&SmsUser=" + HttpUtility.UrlEncode(SmsUser) + "";
CookieContainer cookieContainer = new CookieContainer();
formUrl = formUrl + "?" + formData;
// 设置提交的相关参数
HttpWebRequest request = WebRequest.Create(formUrl) as HttpWebRequest;
request.Method = "GET";
request.KeepAlive = false;
request.AllowAutoRedirect = true;
request.ContentType = "application/x-www-form-urlencoded";
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
request.CookieContainer = cookieContainer;
HttpWebResponse SendSMSResponse = (HttpWebResponse)request.GetResponse();
StreamReader SendSMSResponseStream = new StreamReader(SendSMSResponse.GetResponseStream());
string strRespone = SendSMSResponseStream.ReadToEnd();
SendSMSResponse.Close();
SendSMSResponseStream.Close();
}
3,调用起来都比较简单的,主要是在传递参数的过程中注意中文的处理,否则会变为乱码,获取返回值当然就使用HttpWebResponse对象了
*/
}
}