Demystify the magical power of HTTP proxy: Let your system break through the restrictions of the intranet

2023.06.05

Demystify the magical power of HTTP proxy: Let your system break through the restrictions of the intranet


Through today's discussion, we learned about the use of "http proxy".  When solving the problem of not being able to directly access external network resources, we can set up a proxy server for indirect access.

Hello everyone, I am your Xiaomi!  Today, I want to talk to you about a technical issue, which is about the use of "http proxy".  Yesterday, I just woke up from my lunch break, and the project manager Jie Ge asked me to pay attention to the deployment progress of one of the projects.  And at this moment, the leader on the client side @ me, asking about the server's inability to access the content of the external network.  Facing the needs of customers, of course We must actively respond, so I began to think about solutions.

Add proxy parameters to the service startup command

First, we try to add proxy parameters in the service startup command, hoping to solve the problem of accessing the external network interface. However, even though we have added the proxy parameter, the situation of calling the external network interface has not improved. This plan can be said to have failed, and it can only mean that it is a bit "fucking the street~".

Add proxy parameters to the code

Since the service startup command cannot realize proxy access to the external network, we can only add proxy parameters in the code instead. For the backend code, we can write:

For the front-end code, it is a similar operation.

Then, we re-deployed the corresponding front-end and back-end services in the server, and the result is gratifying that the system can access the external interface normally! Now it's finally done~

what is http proxy

So, let's take a brief look at what an "http proxy" is. An HTTP proxy is a server that acts as a middleman, forwarding requests and responses between a client and a target server. When our system is in the internal network environment and cannot directly access the external network, we can access external network resources through HTTP proxy. A proxy server takes a request from a client, forwards it to a target server, and then forwards the target server's response to the client. In this way, we can indirectly access external network resources.

When to use a proxy

So, under what circumstances do we need to use a proxy? Usually there are several situations:

  • The internal network environment cannot directly access external network resources: When our system is deployed in the internal network environment and cannot directly access external network resources, indirect access can be achieved through a proxy server.
  • Security considerations: The proxy server can play a certain role in security protection, and can filter some malicious requests, block some dangerous websites, etc., thereby improving the security of the system.
  • Traffic control and caching: The proxy server can control and cache traffic, reduce network bandwidth consumption, and improve system performance and response speed.

How to add proxy to java service

Next, let me share how to add agents to Java services. We can do this in two ways:

  • Use the setProperty() method of the System class: You can use the System.setProperty() method to set proxy parameters in the code, so that when a Java program initiates an HTTP request, it will access external resources through the set proxy server. Examples are as follows:
  • System.setProperty("http.proxyHost", "proxy.example.com");
  • System.setProperty("http.proxyPort", "8888");
  • Use third-party libraries: In addition to using the setProperty() method of the System class, we can also use some third-party libraries to implement proxy functions, such as Apache HttpClient and so on. These libraries provide more functions and configuration options to meet different proxy needs.

Summarize

Through today's discussion, we learned about the use of "http proxy". When solving the problem of not being able to directly access external network resources, we can set up a proxy server for indirect access. Either adding proxy parameters to the service startup command or setting the proxy in the code is an effective way to solve this problem. Of course, there are other advantages to using a proxy, such as increased security, traffic control, and caching. In the Java service, we can implement the proxy function through the setProperty() method of the System class or a third-party library.