Asp.net HTTP submits XML data with authentication

Source: Internet
Author: User

I. background

Recently, the company launched a PLM system named PTC in the United States. Because the internal business and other aspects need to achieve data interaction between the PLM system and other original systems. Currently, the solution officially provided by PLM is to import and export data through Excel for Data Interaction. However, this solution is rejected in terms of timeliness, efficiency, and convenience. Initially, I planned to use WebService to build WebService in flexplm, but the official team responded that all their secondary development has never used WebService, so it is difficult to implement it. Finally, I finally proposed a third solution. I submitted data through the HTTP protocol and called the API to exchange data.

Someone may ask why not to directly operate the database? Amount ...... For a database system with 18 tables involved in an operation, it is quite difficult to parse it. You can still use the API. Although it seems like a lot of twists and turns, there is no way to achieve it, there is a gap between laomei's software design philosophy and ours.

Ii. ASP. NET end (implementation of sending request end)

① Prepare the XML format. The sender and receiver agree the XML format. The content is omitted here ....

② Code implementation

Protected string sendrequestwhy (string strxml)
{
String STR = "";
// XML format agreed by both parties
Encoding encoding = encoding. utf8;
// Receiving page string strurl = "http: // 192.168.0.46: 8008/windchill/RF/JSP/material/gateway. jsp ";

Byte [] DATA = encoding. getbytes (strxml );
// Prepare the request...
Httpwebrequest myrequest = (httpwebrequest) webrequest. Create (strurl );
Myrequest. method = "Post ";
Myrequest. contenttype = "text/XML; charset = UTF-8 ";
Myrequest. contentlength = data. length;
// Identity authentication. Pay special attention to the parameter username and password.
Networkcredential cred = new networkcredential ("wcadmin", "wcadmin ");
Myrequest. Credentials = cred;
// Add verification information to the information request header. Otherwise, the verification fails.
Myrequest. preauthenticate = true;
Stream newstream = myrequest. getrequeststream ();
// Send data
Newstream. Write (data, 0, Data. Length );
Newstream. Close ();
Webresponse response = myrequest. getresponse ();
Stream resstream = response. getresponsestream ();
Streamreader sr = new streamreader (resstream, system. Text. encoding. utf8 );
STR = Sr. readtoend (); // receives the returned value. The returned value can be XML.
Resstream. Close ();
Sr. Close ();
Return STR;
}

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.