Using non. Net Web services in asp.net development

Source: Internet
Author: User
Tags abstract bool tostring web services
The Asp.net|web|web service generates a POST request on a URI using the WebRequest and WebResponse classes, which is described in detail in Microsoft's. NET QuickStart Tutorials (Http://chs.gotdotnet.com/qui ckstart/howto/doc/webrequests/clientpost.aspx), briefly cited below:

The WebResponse class is an abstract (MustInherit in Visual Basic) base class in which protocol-specific response classes derive from the abstract base class. Applications can use instances of the WebResponse class to participate in request and response transactions in a protocol-agnostic manner, while protocol-specific classes derived from WebResponse carry the details of the request.

Instead of creating the WebResponse object directly, the client application creates it by calling the GetResponse method on the WebRequest instance.

Description of Inheritors: When inheriting from WebResponse, the following members must be overridden: ContentLength, ContentType, GetResponseStream, ResponseUri, and Headers.

If you need to use the WebRequest and WebResponse classes to generate a GET request on the URI. Specific details of the build request can be found in the function GetPage. The GetPage function takes a string parameter, which is the URL (or URI) of the Web page that you requested. This URI is then included as a parameter in the call to WebRequest.Create, which creates the WebRequest object. The GetResponse function of the WebRequest object is then used to get the WebResponse object. This object can be used to get the status code of the response and the actual response flow (for example, a Web page). The stream can be written in several different forms.

After processing the response stream, you must ensure that the Close method of the WebResponse object is invoked to avoid revealing valuable system resources.

If you need to use the WebRequest and WebResponse classes to generate POST requests on the URI. You can use a simple WebRequest similar to a GET predicate. Two of them are different:
1 The predicate needs to be changed to POST.
2 The format information needs to be encoded and sent to the stream.
To change the predicate, simply set the method property to "POST." You must then set the ContentType property to "application/x-www-form-urlencoded". At this point, make sure that the supplied string is encoded correctly and that all content is passed (POST) correctly.

After processing the response stream, you must ensure that the Close method of the WebResponse object is invoked to avoid revealing valuable system resources.

<!--body-->

After understanding the concrete realization method and the principle, can realize its goal. First you need a function to implement this feature:

<summary>
GetPage get Post Request results page
Address to be requested by the URL
Payload Input Parameters <xml>
OUTPUTRESP Output Results
</summary>
private bool GetPage (string URL, string payload, ref string OUTPUTRESP)
{
WebResponse result = null;
Outputresp = "";
Try
{
WebRequest req = webrequest.create (URL);
Req. method = "POST";
Req. ContentType = "application/x-www-form-urlencoded";
if (payload!= null)
{
string urlencoded = payload;

byte[] somebytes = null;
Somebytes = Encoding.UTF8.GetBytes (urlencoded.tostring ());
Req. ContentLength = Somebytes.length;
Stream newstream = req. GetRequestStream ();
Newstream.write (somebytes, 0, somebytes.length);
Newstream.close ();
}
Else
{
Req. contentlength = 0;
}
result = req. GetResponse ();
Stream Receivestream = result. GetResponseStream ();
Encoding encode = System.Text.Encoding.GetEncoding ("Utf-8");
StreamReader sr = new StreamReader (receivestream, encode);
Outputresp = Sr. ReadToEnd ();
}
catch (Exception e)
{
Console.WriteLine (E.tostring ());
Console.WriteLine ("\ r \ n cannot find the request URI, or it is not in the correct format");
return false;
}
Finally
{
if (Result!= null)
{
Result. Close ();
}
}
return true;
}

This function implements the specific request function, requests the request the address (URL) and the parameter (payload:xml file format), then sends the POST request WebRequest to the service address, reads the concrete result by the StreamReader, The following is a specific example of using this function:

private void Button_serverclick (object sender, System.EventArgs e)
{
String userregisterreq = "";
Userregisterreq = @ "<?xml version=" "1.0" "encoding=" "GB2312" "?>"
<ELink>
<MsgType>UserRegisterReq</MsgType>
<Version>1.0</Version>
<UserProfile>
<UserName>Test</UserName>
<UserPwd>Test</UserPwd>
</UserProfile>
</ELink> ";
String outputresp = "";
BOOL Isreg = false;
Isreg = This.getpage (@ "http://dll.test.com/test.dll/", Userregisterreq, ref OUTPUTRESP);

if (Isreg)
{
OUTPUTRESP = Outputresp.replace (@ "<", "<");
OUTPUTRESP = Outputresp.replace (@ ">", ">");
Response.Write (OUTPUTRESP);
}
}

Button_serverclick is a Button Click event that gets the return value provided by the service program when the correct URL (request address) and payload (input parameters) are entered.




Related Article

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.