Use httpurlconnecion to send a POST request to the proxy email server through nginx

Source: Internet
Author: User
Tags urlencode

Step 1: Get the mail parameters and send the request parameters after urlencode and base64 encoding.

In the request parameters, how do I send large attachments as a request?

First, convert the mail content to a byte array, and then convert it into a byte array for binary operations. This keeps the original appearance of the attachment and will not be affected by any other factors.

Byte [] ATT = attachment. getcontent (); // content of the Attachment

// Encrypted transmission using base64, although not encrypted enough

Base64 base64 = new base64 (); // functions of org. Apache. commons. codec. Binary. base64 and base64

// Encrypted string

String attstr = new string (base64.encode (attachment. getfilecontent ()));

Stringbuilder sb = new stringbuilder ();

// Basic email information
SB. append ("from ="). append (from). append ("& to ="). append ()
. Append ("& Pwd ="). append (password). append ("& CC ="). append (cc)
. Append ("& subject = "). append (urlencoder. encode (subject, "UTF-8 ")). append ("& content = "). append (urlencoder. encode (content, "UTF-8 "))
. Append ("& attachname = "). append (urlencoder. encode (attachname, "UTF-8 ")). append ("& attachfile = "). append (urlencoder. encode (attachcontent, "UTF-8 "));

PS: Because the attachment content may contain URL sensitive characters such as "+", it is best to use urlencode for UTF-8 transcoding before transmission to filter such characters.

Using UTF-8 urlencoding can prevent URL parameter garbled characters or filter sensitive characters.

// String url = "http: // 192.168.10.1: 8090/"; // email server address

 

Then httpurlconntion is connected.

/**
* Method for sending a URL request
* @ Param urlstr request URL
* @ Param content: parameters included in the request
* @ Return the Page code returned by the requested URL
*/
Public String sendbyurl (string urlstr, string content ){
Stringbuilder tempstr = NULL;
Httpurlconnection urlconn = NULL;
Try {
URL url = new URL (urlstr );
// Open the connection
Urlconn = (httpurlconnection) URL. openconnection ();
Urlconn. setdoinput (true); // create an input stream
Urlconn. setdooutput (true); // set the output to URL. The parameter must be placed in the HTTP body. Therefore, set it to true.
Urlconn. setusecaches (false );
// Set the submission method. The default value is get.
Urlconn. setrequestmethod ("Post ");

Outputstream out = urlconn. getoutputstream ();
// Send the parameter body to the URL
Out. Write (content. getbytes ());
Out. Flush ();
Out. Close ();
// Read the returned information
Inputstream in = urlconn. getinputstream ();
Bufferedreader RD = new bufferedreader (New inputstreamreader (in ));
Tempstr = new stringbuilder ("");
While (RD. Read ()! =-1 ){
Tempstr. append (RD. Readline ());
}
Rd. Close ();
In. Close ();
} Catch (exception e ){

E. printstack ();

} Finally {
If (urlconn! = NULL)
Urlconn. Disconnect ();
}
Return tempstr. tostring ();
}

 

Then the nginx proxy is used. Due to company restrictions, the proxy is forwarded twice,

Intranet 10.1 can access Intranet 10.2, Intranet 10.2 can access Internet 24.2, 10.1 can not directly access 24.2, only through 10.2 call 24.2 mail server to send out.

The most pure configuration downloaded from the official nginx website

First 192.168.10.1:

Server {

Listen 8090; // listening for the request Port

SERVER_NAME 192.168.10.1; // host to be monitored
# Charset koi8-r;
# Access_log logs/host. Access. Log main;
Location /{
Proxy_set_header host $ host;/requested host
Proxy_set_header X-real-IP $ remote_addr; // The requested Host IP Address
Proxy_set_header X-forwarded-for $ proxy_add_x_forwarded_for;
Proxy_pass http: // 192.168.10.2: 8090; // host that receives the forwarding request
Root HTML;
Index index.html index.htm;
}

......

 

Second:

Server {

Listen 8090; // listening for the request Port

SERVER_NAME 192.168.10.2; // host to be monitored
# Charset koi8-r; // Request Encoding Method
# Access_log logs/host. Access. Log main; // log of successful connection
Location /{
Proxy_set_header host $ host; // requested host
Proxy_set_header X-real-IP $ remote_addr; // The requested Host IP Address
Proxy_set_header X-forwarded-for $ proxy_add_x_forwarded_for;
Proxy_pass http: // 2018.24.2: 8090; // host that receives the forwarding request
Root HTML;
Index index.html index.htm;
}

......

 

Because it is a POST request, there is basically no need to worry about the request length, but there are limits on the mail server side, just remove the restrictions, after all, the size of uploaded attachments is also limited.

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.