M trying to connect to a site using the following code. But not able to. Please help

this.req = (HttpWebRequest)WebRequest.Create("");
this.req.CookieContainer = new CookieContainer();
this.req.AllowAutoRedirect = false;
this.req.Method = "POST";
this.req.ContentType = "application/x-www-form-urlencoded";
this.strNewValue = "username=" + ckuser + "&password=" + ckpass;
this.req.ContentLength = this.strNewValue.Length;
StreamWriter writer = new StreamWriter(this.req.GetRequestStream(), Encoding.ASCII);
writer.Write(this.strNewValue);
writer.Close();
                
this.response = (HttpWebResponse)this.req.GetResponse();
this.cookieCntr = this.req.CookieContainer;
this.response.Close();
this.req = (HttpWebRequest)WebRequest.Create("");
                this.req.CookieContainer = this.cookieCntr;

this.req.Method = "GET";
this.response = (HttpWebResponse)this.req.GetResponse();

responseee = new StreamReader(this.response.GetResponseStream()).ReadToEnd();
int index = Regex.Match(responseee, "custf").Index;
responseee = responseee.Substring(index, 0x12);
responseee = responseee.Replace("\"", "").Replace(">", "").Trim();

this.response.Close();
lblErrormsg.Text = "connected";

Dani AI

Generated

The thread shows two distinct problems: a proxy authentication failure (HTTP 407) on the first GetResponse, then a 404 after the web.config change. posted the original code and errors, asked for the error, and suggested redirects (not applicable for server-to-server logins). The likely causes are (A) a corporate/proxy that requires credentials, (B) an incorrect target URL or missing form parameters, or (C) the request not matching what the site’s real login form actually sends (hidden fields, tokens, headers, redirects).

Fixes and practical checks

  • Proxy: either enable the system proxy in config or set proxy credentials programmatically so the 407 is handled by the runtime. Example (programmatic):

    WebRequest.DefaultWebProxy.Credentials = CredentialCache.DefaultNetworkCredentials;
    var req = (HttpWebRequest)WebRequest.Create(loginUrl);
    req.Proxy = WebRequest.DefaultWebProxy;
    req.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials;

    Or, in web.config make sure the proxy is placed under <system.net> and usesystemdefault="true" if the machine has a configured proxy.

  • Build the POST correctly: set ContentLength from the byte count, not string length, and write raw bytes so the server sees an exact payload:

    byte[] payload = Encoding.ASCII.GetBytes(postData);
    req.ContentLength = payload.Length;
    using (var s = req.GetRequestStream()) s.Write(payload, 0, payload.Length);
  • Cookies, redirects and headers: use a shared CookieContainer, set reasonable headers (User-Agent, Referer, Accept), and either allow auto-redirect or explicitly inspect 3xx responses and the Location header. Read a WebException.Response to see server details instead of swallowing exceptions.

  • Confirm the real form: capture a login with browser DevTools (Network) to copy the exact action URL, POST field names, hidden tokens and required headers. A small typo in the host/path (for example an extra character in the hostname) will cause 404.

If server-to-server automation remains blocked, consider the modern HttpClient approach with HttpClientHandler (cookie container and proxy support) as an easier-to-control alternative. A short debug checklist: verify the exact URL, compare POST body and headers to a real browser login, enable proxy credentials, inspect response.StatusCode and headers, and log the raw request/response for comparison.

Recommended Answers

All 6 Replies

Member Avatar for Member #905211

Are you getting an error message? If you are, what is the error?

yes. M getting error 407, proxy error on line no. 12, i.e.,
this.response = (HttpWebResponse)this.req.GetResponse();

I added followin code in web.config

<defaultProxy>
<proxy bypassonlocal="True" usesystemdefault="False"  />  
</defaultProxy>

and changed cs code to as given below

this.req = (HttpWebRequest)WebRequest.Create("");
this.req.CookieContainer = new CookieContainer();
                this.req.AllowAutoRedirect = false;
                this.req.Method = "POST";
                this.req.ContentType = "application/x-www-form-urlencoded";
                this.strNewValue = "username=" + ckuser + "&password=" + ckpass;
                this.req.ContentLength = this.strNewValue.Length;
                StreamWriter writer = new StreamWriter(this.req.GetRequestStream(), Encoding.ASCII);
                writer.Write(this.strNewValue);
                writer.Close();
 using (WebResponse res = req.GetResponse()) //error
                { 
                    response = (HttpWebResponse)res;
                    
                    Console.WriteLine(response.StatusCode); 
                }


                this.cookieCntr = this.req.CookieContainer;
                this.response.Close();
                this.req = (HttpWebRequest)WebRequest.Create("");
                this.req.CookieContainer = this.cookieCntr;
                this.req.Method = "GET";
                this.response = (HttpWebResponse)this.req.GetResponse();
                responseee = new StreamReader(this.response.GetResponseStream()).ReadToEnd();
                int index = Regex.Match(responseee, "custf").Index;
                responseee = responseee.Substring(index, 0x12);
                responseee = responseee.Replace("\"", "").Replace(">", "").Trim();
                this.response.Close();
                lblErrormsg.Text = "connected";
            }
            catch (Exception)
            {
                lblErrormsg.Text = "Error connecting to the server...";
            }

Web exception is caught saying 'The remote server returned an error: (404) Not Found.'

FOR SIMPLY CONNECTING To ANOTHER SITE,

Use

Server.Tranfer("");

OR

Response.Redirect("",true);

I am connection to way2sms. I need to send message to amy number through this site, using my asp.net application.
response.redirect will redirect me to that site. I dont want that.

Recheck the API

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.