WinForm Access URL parameter has a return value

Source: Internet
Author: User

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 ();
Processing the returned text here is OK, such as:

StreamReader Reader = new StreamReader (s, Encoding.UTF8);
while ((strdate = Reader.readline ()) = null)
{
strvalue + = strdate + "\ r \ n";
}
}
Console.WriteLine (strvalue);
Console.readkey ();

}
/*
WinForm passing parameters to URLs and getting return values

In the WinForm call URL and pass parameters mainly use HttpWebRequest object, divided into two forms of post and get, personal feeling is to simulate the browser,

1.post Method:

<summary>
Using the Post method, call the SMS interface
</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 address
String formData = "phonenumber=" + PhoneNumber + "&smscontent=" +smscontent + "&phonenumbertype=" + Phonenumbertyp E + "&smsuser=" + Smsuser + "";
Cookiecontainer Cookiecontainer = new Cookiecontainer ();
Converts a committed string data into a byte array
byte[] PostData = Encoding.UTF8.GetBytes (FormData);

Set the relevant parameters for the submission
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;

Submit Request Data
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 method

//<summary>
//Use the Get method
//</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 = "phonenumber=" + httputility.urlencode (phonenumber) + "&smscontent=" + httputility.urlencode ( smscontent) + "&phonenumbertype=" + httputility.urlencode (phonenumbertype) + "&smsuser=" + Httputility.urlencode (Smsuser) + "";
Cookiecontainer Cookiecontainer = new Cookiecontainer ();

Formurl = Formurl + "?" + FormData;
Set the relevant parameters for the submission
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, the call is relatively simple, mainly in the process of passing parameters to pay attention to the processing of Chinese, otherwise it will become garbled, get the return value of course, using the HttpWebResponse object
*/
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.