का उपयोग कर वेब प्रॉक्सी के माध्यम से विफल रहता है मेरे पास एक डेस्कटॉप क्लाइंट है जो वेब सर्वर पर डेटा भेजता है और मुझे प्रॉक्सी सर्वर से नहीं मिल रहा है।जावा संचार अपाचे एचटीपी क्लाइंट
अद्यतन: प्रॉक्सी के माध्यम से संवाद करने की कोशिश करते समय मुझे 407 HTTP त्रुटि मिल रही है।
मेरे वेब सर्वर से जानकारी डाउनलोड करते समय, सब कुछ ठीक है। एक बार उपयोगकर्ता प्रॉक्सी सर्वर को कॉन्फ़िगर करता है (मैंने लिखा एक संवाद बॉक्स का उपयोग करके) डाउनलोड ठीक काम करता है। लेकिन org.apache.http.client.HttpClient का उपयोग कर डेटा अपलोड करना काम नहीं कर रहा है।
मैं JDialog से जानकारी एकत्र करने के बाद प्रॉक्सी सर्वर को इस तरह कोड के साथ कॉन्फ़िगर कर रहा हूं।
System.setProperty("http.proxyHost", proxyHost);
System.setProperty("http.proxyPort", "" + portNumber);
अब, एक बार ऐसा करने के बाद, सरल डाउनलोड ठीक काम करते हैं। उदाहरण के लिए, मेरे पास कोड है जो मेरे वेब सर्वर से कुछ XML डेटा पढ़ता है (नीचे देखें)। ग्राहक के नेटवर्क पर प्रॉक्सी सेटिंग्स कॉन्फ़िगर किए जाने से पहले मेरे कैच ब्लॉक में त्रुटि प्रदर्शित की गई थी और फिर सही प्रॉक्सी सेट होने के बाद सबकुछ ठीक काम करता था।
/**
* Loads a collection of exams from the web site. The URL is determined by
* configuration or registration since it is State specific.
*/
public static int importExamsWS(StringBuilder msg) {
try {
java.net.URL onlineExams = new URL(examURL);
//Parse the XML data from InputStream and store it.
return importExams(onlineExams.openStream(), msg);
}
catch (java.net.UnknownHostException noDNS) {
showError(noDNS, "Unable to connect to proctinator.com to download the exam file.\n"
+ "There is probably a problem with your Internet proxy settings.");
}
catch (MalformedURLException | IOException duh) {
showFileError(duh);
}
return 0;
}
हालांकि, यह के रूप में यदि प्रॉक्सी सेटिंग्स पर ध्यान नहीं दिया जा रहा है और एक IOException फेंक दिया जाता है जब मैं वेब सर्वर से डेटा भेजने के लिए प्रयास करें। अर्थात्:
org.apache.http.conn.HttpHostConnectException: Connection to http://proctinator.com:8080 refused
अब मुझे पता है कि पोर्ट 8080 ग्राहक की वेब फिल्टर द्वारा अवरोधित नहीं है क्योंकि हम एक वेब ब्राउज़र में पता का परीक्षण किया।
उपयोगकर्ता द्वारा दर्ज पंजीकरण आईडी की पुष्टि करने के लिए मेरा कोड यहां दिया गया है: अपडेट: अब मैं इस विधि में प्रॉक्सी भी सेट कर रहा हूं।
//Registered is just an enum with ACTIVE, INACTIVE, NOTFOUND, ERROR
public static Registered checkRegistration(int id) throws IOException {
httpclient = new DefaultHttpClient();
Config pref = Config.getConfig(); //stores user-entered proxy settings.
HttpHost proxy = new HttpHost(pref.getProxyServer(), pref.getProxyPort());
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
URIBuilder builder = new URIBuilder();
String path = "/Proctorest/rsc/register/" + id;
try {
builder.setScheme("http").setHost(server).setPort(8080).setPath(path);
URI uri = builder.build();
System.out.println("Connecting to " + uri.toString());
HttpGet httpget = new HttpGet(uri);
HttpResponse response = httpclient.execute(httpget);
System.out.println(response.getStatusLine().toString());
if(response.getStatusLine().getStatusCode()==200) {
String msg = EntityUtils.toString(response.getEntity());
GUI.globalLog.log(Level.INFO, "Server response to checkRegistration(" + id + "): " + msg);
return Registered.stringToRegistered(msg);
}
else {
GUI.globalLog.log(Level.INFO, "Server response status code to checkRegistration: " +
response.getStatusLine().getStatusCode());
return Registered.ERROR;
}
}
catch(java.net.URISyntaxException bad) {
System.out.println("URI construction error: " + bad.toString());
return Registered.ERROR;
}
}
मैं लगभग सकारात्मक हूँ कि समस्या प्रॉक्सी सर्वर विन्यास से आ रही है, फिर भी docs for SystemDefaultHttpClient दावा है कि यह लेता है http.proxyHost
और http.proxyPort
के लिए सिस्टम गुण का उपयोग करता है। मुझे पता है कि गुण ठीक से सेट किए गए हैं, लेकिन मुझे अभी भी यह प्रमाणीकरण त्रुटि मिल रही है। मेरे कार्यक्रम से उत्पन्न लॉग को देखते हुए मुझे यह दिखाया गया:
checkRegistration INFO: Server response status code to checkRegistration: 407
मैं इस प्रमाणीकरण त्रुटि को कैसे हल करूं?
आप डेटा कैसे भेजते हैं? एक पद? एक पुट? पैरामीटर के साथ एक मिलता है? क्षमा करें, मैं आपके प्रश्न से नहीं पता कर सकता। –
हम यहां जीईटी का उपयोग कर रहे हैं। अन्य विधियों को पोस्ट का उपयोग नहीं दिखाया गया है। उपरोक्त कोड में HttpGet httpget = new HttpGet (uri); – Thorn
क्या आप अपने देव पर्यावरण में प्रॉक्सी स्थापित कर सकते हैं, फिर अपनी देव मशीन पर, प्रॉक्सी के अलावा सभी आउटगोइंग http को अवरुद्ध करने के लिए फ़ायरवॉल का उपयोग कर सकते हैं? – artbristol