openConnection(proxy) રૂટ સાથે કનેક્શન બનાવે છે. વિનંતી શરૂ કરતા પહેલાં પરત મળેલા HttpURLConnection પર HTTP પદ્ધતિ અને બોડી સેટ કરો:

Proxy route = new Proxy(Proxy.Type.HTTP,
    new InetSocketAddress("proxy.example", 8080));
HttpURLConnection connection = (HttpURLConnection)
    URI.create("http://www.example.com/submit").toURL().openConnection(route);

try {
    connection.setConnectTimeout(5000);
    connection.setReadTimeout(15000);
    connection.setRequestMethod("POST");
    connection.setDoOutput(true);
    connection.setRequestProperty("Content-Type",
        "application/x-www-form-urlencoded; charset=UTF-8");

    byte[] form = "message=hello+proxy".getBytes(StandardCharsets.UTF_8);
    try (OutputStream out = connection.getOutputStream()) {
        out.write(form);
    }

    int status = connection.getResponseCode();
    System.out.println(status);
    InputStream response = status >= 400
        ? connection.getErrorStream() : connection.getInputStream();
    if (response != null) response.close();
} finally {
    connection.disconnect();
}

ઉદાહરણ નાનું ફોર્મ મોકલે છે, તેની સ્થિતિ છાપે છે અને વણવપરાયેલ પ્રતિસાદ સ્ટ્રીમ બંધ કરે છે. બોડીની જરૂર હોય ત્યારે તે છેલ્લા પગલાને એપ્લિકેશનની પ્રતિસાદ-બોડી હેન્ડલિંગ સાથે બદલો.

પ્રોક્સીનું હોસ્ટનામ અને પોર્ટ route ના છે. પ્રોક્સી ખાતા વિના માન્ય જાહેર ફોર્મ પરીક્ષણ માટે HTTP પ્રોક્સી સૂચિ ના એક એન્ટ્રીમાંથી HTTP સરનામું પસંદ કરો. URL ફોર્મનું અંતિમ બિંદુ જ રહે છે. ગતિશીલ ફોર્મ મૂલ્યો માટે URLEncoder વાપરીને દરેક મૂલ્ય એન્કોડ કરો, અનએસ્કેપ્ડ ઇનપુટને સમાન ચિહ્ન અને એમ્પરસેન્ડ સાથે જોડશો નહીં.

આઉટપુટ સ્ટ્રીમ તૈયાર બાઇટ્સ મોકલે છે. લખ્યા અને બંધ કર્યા પછી સ્થિતિ વાંચો, પછી યોગ્ય સામાન્ય કે ભૂલ પ્રતિસાદ સ્ટ્રીમ પસંદ કરો. નિષ્ફળ HTTP પ્રતિસાદમાં એપ્લિકેશન કે પ્રોક્સીની ઉપયોગી ભૂલ સામગ્રી હોઈ શકે છે.

સંબંધિત લેખો