Is my method throwing me an exception because the proxy is dead? - c#

So I've been getting into WebRequests recently and something that I found pretty interesting was making a connection through a proxy.
I looked at some blog posts for some code to get a general idea on how it worked and the most recent one would be this code snippet right here.
private static void requestProxy()
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://google.com");
WebProxy myproxy = new WebProxy("77.121.11.33", 1080);
myproxy.BypassProxyOnLocal = false;
request.Proxy = myproxy;
request.Method = "GET";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
string content = sr.ReadToEnd();
Debug.Print(content);
Console.WriteLine(content);
}
Console.ReadLine();
}
I tried making a request using that but it seems that it's throwing me an error everytime I try to use a proxy. However when I am not using a proxy it's not throwing me any errors, so I am assuming that the problem lays within the proxy. I tried using different ones but no go.
What I am doing wrong here and whats going on? How do I make a connection with a proxy properly?
Error message
System.Net.WebException: 'An error occurred while sending the request.
The server returned an invalid or unrecognized response'
https://imgur.com/ThxNWzb

Related

GetResponse() in API call causes (400) Bad Request error

I am very new to programming.
I'm setting up a loop that continuously send a POST request to a site through the REST API. The POST request works properly and the way I intend.
I would like to add functionality that requires the response from this post. However, every way of retrieving the response gives me an error:
"An unhandled exception of type 'System.Net.WebException' occurred in System.dll
Additional information: The remote server returned an error: (400) Bad Request."
As I debug line by line, it seems that the "GetResponse()" line causes this each time, and causes the program to break. If I remove this line, the program works properly and throws no errors.
Hoping someone can assist. Here is what I have written:
string URL= "https:.......";
HttpWebRequest apiCall = (HttpWebRequest)WebRequest.Create(airWatchURL);
apiCall.Method = "POST";
apiCall.Headers.Add(HttpRequestHeader.Authorization, auth);
apiCall.Headers.Add("aw-tenant-code", apiKey);
apiCall.ContentType = "application/json";
apiCall.ContentLength = noBody.Length; //noBody = empty string
Stream c = apiCall.GetRequestStream();
Encoding d = System.Text.Encoding.ASCII;
StreamWriter requestWriter = new StreamWriter(c, d);
requestWriter.Write(noBody);
requestWriter.Close();
WebResponse apiResponse = apiCall.GetResponse(); //This line will return an error.
This will also return the same error:
HttpWebResponse apiResponse = apiCall.GetResponse() as HttpWebResponse;
This will again return the same error:
string status;
using (HttpWebResponse response = apiCall.GetResponse() as HttpWebResponse)
{
status = response.StatusCode.ToString();
}
I just cant seem to correctly call the GetResponse Method.

While debugging this code fragment ,this error occurs

While debugging this code fragment ,this error(Unable to connect remote server) occurs.I showed where error occured in comment line
private static string GetWebText(string url)
{
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.UserAgent = "A .NET Web Crawler";
WebResponse response = request.GetResponse();//errors occured here
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream);
string htmlText = reader.ReadToEnd();
return htmlText;
}
You should try to figuere out what is wrong, it can be wrong url or something that goes wrong with the request / response. In order to look at the traffic behind your application I would suggest using Fiddler for checking what happens behind the scene.

Protocol violation using Github api

I'm getting data from Github for my application.
The first 2 OAuth steps are ok, but in the third I got the following error:
"the server committed a protocol violation. Section=ResponseStatusLine ERROR"
This is my code:
protected String WebRequest(string url)
{
url += (String.IsNullOrEmpty(new Uri(url).Query) ? "?" : "&") + "access_token=" + _accessToken;
HttpWebRequest webRequest = System.Net.WebRequest.Create(url) as HttpWebRequest;
webRequest.Method = "GET";
webRequest.ServicePoint.Expect100Continue = false;
try
{
using (StreamReader responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream()))
return responseReader.ReadToEnd();
}
catch
{
return String.Empty;
}
}
The program goes in exception after using the StreamReader object, that returns me the error.
If I follow these instructions The server committed a protocol violation. Section=ResponseStatusLine ERROR , the error turns into "403 forbidden".
When Github used api V2, different from now, there was no problem with this code.
So, can't be a .NET limitation but something connected with Github server.
Any suggestions?
You need to set UserAgent like this:
webRequest.UserAgent = "YourAppName";
Otherwise it will give The server committed a protocol violation. Section=ResponseStatusLine error.

.NET service responds 500 internal error and "missing parameter" to HttpWebRequest POSTS but test form works fine

I am using a simple .NET service (asmx) that works fine when invoking via the test form (POST). When invoking via a HttpWebRequest object, I get a WebException "System.Net.WebException: The remote server returned an error: (500) Internal Server Error." Digging deeper, reading the WebException.Response.GetResponseStream() I get the message: "Missing parameter: serviceType." but I've clearly included this parameter.
I'm at a loss here, and its worse that I don't have access to debug the service itself.
Here is the code being used to make the request:
string postData = String.Format("serviceType={0}&SaleID={1}&Zip={2}", request.service, request.saleId, request.postalCode);
byte[] data = (new ASCIIEncoding()).GetBytes(postData);
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.Timeout = 60000;
httpWebRequest.Method = "POST";
httpWebRequest.ContentType = "application/x-www-form-urlencoded";
httpWebRequest.ContentLength = data.Length;
using (Stream newStream = httpWebRequest.GetRequestStream())
{
newStream.Write(data, 0, data.Length);
}
try
{
using (response = (HttpWebResponse)httpWebRequest.GetResponse())
{
if (response.StatusCode != HttpStatusCode.OK)
throw new Exception("There was an error with the shipping freight service.");
string responseData;
using (StreamReader responseStream = new StreamReader(httpWebRequest.GetResponse().GetResponseStream(), System.Text.Encoding.GetEncoding("iso-8859-1")))
{
responseData = responseStream.ReadToEnd();
responseStream.Close();
}
if (string.IsNullOrEmpty(responseData))
throw new Exception("There was an error with the shipping freight service. Request went through but response is empty.");
XmlDocument providerResponse = new XmlDocument();
providerResponse.LoadXml(responseData);
return providerResponse;
}
}
catch (WebException webExp)
{
string exMessage = webExp.Message;
if (webExp.Response != null)
{
using (StreamReader responseReader = new StreamReader(webExp.Response.GetResponseStream()))
{
exMessage = responseReader.ReadToEnd();
}
}
throw new Exception(exMessage);
}
Anyone have an idea what could be happening?
Thanks.
UPDATE
Stepping through the debugger, I see the parameters are correct. I also see the parameters are correct in fiddler.
Examining fiddler, I get 2 requests each time this code executes. The first request is a post that sends the parameters. It gets a 301 response code with a "Document Moved Object Moved This document may be found here" message. The second request is a GET to the same URL with no body. It gets a 500 server error with "Missing parameter: serviceType." message.
It seems like you found your problem when you looked at the requests in Fiddler. Taking an excerpt from http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html:
10.3.2 301 Moved Permanently
The requested resource has been assigned a new permanent URI and any future references to this resource SHOULD use one of the returned URIs. Clients with link editing capabilities ought to automatically re-link references to the Request-URI to one or more of the new references returned by the server, where possible.
.....
Note: When automatically redirecting a POST request after
receiving a 301 status code, some existing HTTP/1.0 user agents
will erroneously change it into a GET request.
Here's a couple options that you can take:
Hard-code your program to use the new Url that you see in the 301 response in Fiddler
Adjust your code to retrieve the 301 response, parse out the new Url from the response, and build a new response with the new Url.
The latter option would be ideal if you're dealing with user-based input on the Url (like a web browser), since you don't know where the user is going to want your program to go.

Webrequest not working in a Windows Service

I am not able to use Webrequest in a windows service. It fails with error ""Unable to connect to the remote server".
WebRequest request = WebRequest.Create(url);
NetworkCredential nc = new NetworkCredential("myuname","mypassword","mydomain");
request.Proxy.Credentials = nc;
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
}
catch (WebException ex)
{
//ex.Message is "Unable to connect to the remote server"
}
The code works perfectly fine if it is a console application.
Could someone please tell if there is a fix?
You might want to try specifying the proxy server on the request object you've created. When you run the console application I believe that the system looks up your IE configuration for you and the proxy may be set. If the service is running under an account other than yours it might not have the proxy set.
I was facing the same problem. After lots of R&D a solution worked for me.
Instead of:
WebResponse responseObject = requestObject.GetResponse();
I wrote:
WebResponse responseObject = null;
responseObject = requestObject.GetResponse();
and it worked fine.
I am also looking for the exact reason behind this behavior.

Categories