2010-01-29 11 views
22

से अधिक HttpWebRequest के साथ एक अनुरोध भेजने मैं क्लासिक एएसपी में निम्नलिखित स्निपेट, एक कमांड भेजने और SSL पर प्रतिक्रिया प्राप्त करने के लिए है:त्रुटि 502 (ख़राब प्रवेश द्वार) जब एसएसएल

Dim xmlHTTP 
Set xmlHTTP = Server.CreateObject("Msxml2.ServerXMLHTTP.3.0") 
xmlHTTP.open "POST", "https://www.example.com", False 
xmlHTTP.setRequestHeader "Content-Type","application/x-www-form-urlencoded" 
xmlHTTP.setRequestHeader "Content-Length", Len(postData) 
xmlHTTP.Send postData 
If xmlHTTP.status = 200 And Len(message) > 0 And Not Err Then 
    Print xmlHTTP.responseText 
End If 

तो मैं एक के रूप में इस्तेमाल किया this code

private static string SendRequest(string url, string postdata) 
{ 
    WebRequest rqst = HttpWebRequest.Create(url); 
    // We have a proxy on the domain, so authentication is required. 
    WebProxy proxy = new WebProxy("myproxy.mydomain.com", 8080); 
    proxy.Credentials = new NetworkCredential("username", "password", "mydomain"); 
    rqst.Proxy = proxy; 
    rqst.Method = "POST"; 
    if (!String.IsNullOrEmpty(postdata)) 
    { 
     rqst.ContentType = "application/x-www-form-urlencoded"; 

     byte[] byteData = Encoding.UTF8.GetBytes(postdata); 
     rqst.ContentLength = byteData.Length; 
     using (Stream postStream = rqst.GetRequestStream()) 
     { 
      postStream.Write(byteData, 0, byteData.Length); 
      postStream.Close(); 
     } 
    } 
    ((HttpWebRequest)rqst).KeepAlive = false; 
    StreamReader rsps = new StreamReader(rqst.GetResponse().GetResponseStream()); 
    string strRsps = rsps.ReadToEnd(); 
    return strRsps; 
} 

समस्या है, जब GetRequestStream बुला मैं संदेश "The remote server returned an error: (502) Bad Gateway."

के साथ एक WebException मिलती रहती है: संदर्भ सी # में अनुरोध reimplement को

पहले मैंने सोचा था कि इसे SSL प्रमाणपत्र सत्यापन के साथ करना था। इसलिए मैं इस लाइन कहा:

ServicePointManager.CertificatePolicy = new AcceptAllCertificatePolicy(); 

कहाँ

public class AcceptAllCertificatePolicy : ICertificatePolicy 
{ 
    public bool CheckValidationResult(ServicePoint srvPoint, 
             System.Security.Cryptography.X509Certificate certificate, 
             WebRequest request, 
             int certificateProblem) 
    { 
     return true; 
    } 
} 

और मैं एक ही 502 त्रुटि मिलती रहती है। कोई विचार?

उत्तर

26

With the help of this मुझे समस्या का एक और विस्तृत विवरण मिला: प्रॉक्सी संदेश लौट रहा था: "उपयोगकर्ता एजेंट पहचाना नहीं गया है।" तो मैंने इसे मैन्युअल रूप से सेट किया। इसके अलावा, मैंने () का उपयोग करने के लिए कोड बदल दिया, जैसा कि here वर्णित है। अंतिम कामकाजी कोड नीचे शामिल है।

private static string SendRequest(string url, string postdata) 
{ 
    if (String.IsNullOrEmpty(postdata)) 
     return null; 
    HttpWebRequest rqst = (HttpWebRequest)HttpWebRequest.Create(url); 
    // No proxy details are required in the code. 
    rqst.Proxy = GlobalProxySelection.GetEmptyWebProxy(); 
    rqst.Method = "POST"; 
    rqst.ContentType = "application/x-www-form-urlencoded"; 
    // In order to solve the problem with the proxy not recognising the user 
    // agent, a default value is provided here. 
    rqst.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)"; 
    byte[] byteData = Encoding.UTF8.GetBytes(postdata); 
    rqst.ContentLength = byteData.Length; 

    using (Stream postStream = rqst.GetRequestStream()) 
    { 
     postStream.Write(byteData, 0, byteData.Length); 
     postStream.Close(); 
    } 
    StreamReader rsps = new StreamReader(rqst.GetResponse().GetResponseStream()); 
    string strRsps = rsps.ReadToEnd(); 
    return strRsps; 
} 
1

यह संभव है कि वेब सेवा के लिए wsdl डोमेन नाम और SSL प्रमाणपत्र के साथ "बहस" हो। आईआईएस आईआईएस पंजीकृत डोमेन नाम का उपयोग कर एक वेब सेवा के डब्लूएसडीएल को स्वत: उत्पन्न करेगा (जो डिफ़ॉल्ट रूप से स्थानीय डोमेन पर मशीन का नाम है, जरूरी नहीं कि आपका वेब डोमेन)। यदि प्रमाण पत्र डोमेन SOAP12 पते में डोमेन से मेल नहीं खाता है तो आपको संचार त्रुटियां मिलेंगी।

28

त्रुटि प्रतिक्रिया के इकाई निकाय को पढ़ें। यह हो रहा है कि क्या हो रहा है के बारे में एक संकेत हो सकता है।

ऐसा करने के लिए कोड इस प्रकार है:

catch(WebException e) 
{ 
if (e.Status == WebExceptionStatus.ProtocolError) 
{ 
    WebResponse resp = e.Response; 
    using(StreamReader sr = new StreamReader(resp.GetResponseStream())) 
    { 
     Response.Write(sr.ReadToEnd()); 
    } 
} 
} 

कि त्रुटि प्रतिसाद की पूरी सामग्री दिखाना चाहिए।

+0

बहुत बढ़िया सलाह:

static void WriteUnderlyingResponse(Exception exception) { do { if (exception.GetType() == typeof(WebException)) { var webException = (WebException)exception; using (var writer = new StreamReader(webException.Response.GetResponseStream())) Console.WriteLine(writer.ReadToEnd()); } exception = exception?.InnerException; } while (exception != null); } 

प्रॉक्सी से प्रतिक्रिया शरीर कुछ इस तरह देखा! इसका उपयोग करके मैंने समस्या के बारे में कुछ और पाया और समाधान के साथ आया। धन्यवाद। – Leonardo

1

UserAgent

उदाहरण के लिए

याद आ रही है: "(; MSIE 7.0, Windows NT 5.1 संगत) मोज़िला/4.0" request.UserAgent =;

0

यह मेरे लिए हो रहा था क्योंकि रिमोट मशीन पर एक जावा प्रॉक्सी समय पर प्रतिक्रिया नहीं दे रही थी, अगर .NET डिफ़ॉल्ट टाइमआउट बेकार की तरह प्रस्तुत करता था। निम्नलिखित कोड सभी अपवादों के माध्यम से लूप और प्रतिक्रियाओं जो मुझे का निर्धारण यह वास्तव में प्रॉक्सी से आ रहा था मदद की बाहर लिखते हैं:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> 
<html><head> 
<title>502 Proxy Error</title> 
</head><body> 
<h1>Proxy Error</h1> 
<p>The proxy server received an invalid 
response from an upstream server.<br /> 
The proxy server could not handle the request <em><a href="/xxx/xxx/xxx">POST&nbsp;/xxx/xxx/xxx</a></em>.<p> 
Reason: <strong>Error reading from remote server</strong></p></p> 
</body></html>