I am creating a C# windows form that is designed to access websites and display their HTML. However, the methodology that I am using seems to only be working with sites that don't include www. (or at least I think that's what's happening.) I'm not very good at this sort of thing, so I apologize if I missed something obvious. Many thanks in advance, here's the code:
string urlAddress = "https://www.pintrest.com"; //using Pinterest for testing
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlAddress);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
The error I keep getting is:
An unhandled exception of type 'System.UriFormatException' occurred in System.dll
Additional information: Invalid URI: The format of the URI could not be determined.
I don't understand what is wrong with that URL. What's going on?
Related
We have a REST API which maps third-party proprietary language codes (in the en-US format) into language codes that our own system recognises. The REST API's route is
~/v1/languages/mappings/{foreignLanguageCode} [GET]
We receive these third-party language codes and send them to our own API using an instance of System.Net.Http.HttpClient.
This is working perfectly well for many language codes but when passed ko-US the request doesn't even get sent - the HttpClient object throws the exception
An error occurred while sending the request.
The inner exception - a WebException - says
The server committed a protocol violation. Section=ResponseStatusLine
The exception also contains
Source: "System"
Status: ServerProtocolViolation
StackTrace:
at System.Net.HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at System.Net.Http.HttpClientHandler.GetResponseCallback(IAsyncResult ar)
The URI at this point is
https://www.example.com/api/v1/languages/mappings/ko-US/
I've tried it with and without that trailing slash: no difference. We can send many other language codes to the same API without any problem.
Can anyone explain why this particular URI causes this exception?
Answering my own question...
I can't really explain why but this solved the problem:
myHttpClient.DefaultRequestHeaders.ConnectionClose = true;
I added that immediately before sending the request, so the working code looks like this:
httpClient.DefaultRequestHeaders.ConnectionClose = true;
var response = await httpClient.GetAsync(uri).ConfigureAwait(false);
I am working on a .Net Standard class library and working on creating a library for the new Destiny 2 Api. I have one method working which is the search for a user to get their information. However, when I make a request to a different endpoint I get this error:
System.AggregateException: One or more errors occurred. --->
System.Net.Http.HttpRequestException: Error while copying content to a
stream. ---> System.IO.IOException: The read operation failed, see
inner exception. ---> System.Net.Http.WinHttpException: The operation
has been canceled
I can send this request just fine using PostMan or any other Api testing tool so something must be wrong with my code. Something worth noting, when using other Api tools I notice that I get redirected a few times. Also when using Fiddler I can see that none of the requests actually get Json back, they all look like redirects to another page, Postman shows 3 redirects fiddler shows 2 and then a failure in my code.
My code is pretty small so I cannot think of much that could be breaking it:
public string GetProfile(BungieMembershipType membershipType, string destinyMembershipId)
{
var properUrl = String.Format(GetProfileUrl, (int)membershipType, destinyMembershipId);
var rawData = RootRequest.Web.GetStringAsync(properUrl).Result;
return rawData;
}
The only thing that seems odd to me is I am testing my code inside of Unit Tests, I cannot evaluate RootRequest during the debugging. RootRequest is a static class that has a static HttpClient on it that is used for making all requests to keep authentication to the Api simple.
I would assume that something is actually wrong internally at the Bungie Api servers but multiple things about my code had to change. Firstly, my BaseUri was missing the www.. Secondly, the GetProfileUrl was missing a / before the ?queryString was added.
I am trying to do a web search from within a C# app. I am currently using this code that gets an error.
WebRequest http = HttpWebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)http.GetResponse(); //error occurs here
I keep getting "The remote name could not be resolved: 'search.yahooapis.com'".
Here is the code for the url parameter:
StringBuilder url = new StringBuilder();
url.Append("http://search.yahooapis.com/WebSearchService/V1/webSearch?");
url.Append("appid=YahooDemo&results=100&query=");
url.Append(HttpUtility.UrlEncode(searchFor));
The problem, I think, is that I need an API key from Yahoo in place of 'YahooDemo' in the above code. I went to http://developer.apps.yahoo.com/projects and got an application ID but when I enter it it still does not work? I think the problem is I did not know what to put in the Yahoo project for Application URL and Callback Domain - I don't really know what this even means? I am happy to use other providers such as Google or Bing if this makes it easier. But I am new to C# so really need detailed but simple explanations to understand what I need to do. I am a bit lost. In the end I basically just want to do a web search from my C# program to look for key words, so if their is an easier way to do this I am all for it. Any suggestions?
I'm developing a Windows Phone 8 app that consumes a web service. This particular web service requires a GET request with an entity body.
I'm using the System.Net.Http.HttpClient to send this request, which I've successfully used to send various other web requests (GETs, POSTs, and PUTs). This is the first GET request that includes a payload and therefore includes a Content-Type and Content-Length header.
The request fails with an exception like the following:
'TaskHost.exe' (CLR C:\windows\system32\coreclr.dll: Silverlight AppDomain): Loaded 'C:\windows\system32\en-US\mscorlib.debug.resources.dll'. Module was built without symbols.
An exception of type 'System.Net.ProtocolViolationException' occurred in System.Windows.ni.dll and wasn't handled before a managed/native boundary
A first chance exception of type 'System.Net.ProtocolViolationException' occurred in mscorlib.ni.dll
An exception of type 'System.Net.ProtocolViolationException' occurred in mscorlib.ni.dll and wasn't handled before a managed/native boundary
Including a payload with a GET request is a bit unusual although according to my research, not expressly forbidden by the RFCs.
I don't have the option of changing the web service. Does System.Net.Http.HttpClient outright not allow a GET with an entity body? If not, are there any work-arounds?
Thank you.
I know it's a while since you asked this but here goes anyway.
While sending a payload in the body of a GET request may not violate the RFC's client-side, it does go against common practice. What the RFC's do say is that a server fielding such a request is in no way obliged to look at anything other than the request URI and the Host: header in order to determine what its response should be.
Quoting from the HTTP 1.1 specification in RFC2616, section 5.2 says: "The exact resource identified by an Internet request is determined by examining both the Request-URI and the Host header field." It says nothing about a request body.
This means that, even if what you're attempting to do is not in violation of any RFC, the web service consumed by your WP8 app is. If you have no option but to use it then it's looking like you're going to have to roll out your own HTTP-ish client, which I'm guessing you've probably already done by now. The off-the-shelf solution in System.Net.Http.HttpClient isn't going to exhibit behaviour that will never be of use while communicating with a compliant server.
I am getting this error:
Client found response content type of 'text/html', but expected 'text/xml.
I am adding web reference for live search. When i build the project its Successful. But after that once i enter some text in textbox & enter search button it gives this error. I am Using my local machine & Using .net 2.0 with C#.
Plz help me...
Thanks In Advance...
As Matt said, it's probably an error page coming back.
Either use a proxy like Fiddler or a network sniffer like WireShark to see what the raw response is - that should help you get to the bottom of what's going on.
Generally that error means that the service has sent back an (HTML) error message rather than the XML SOAP response that your client was expecting.
For web services that you control it's really easy to find the problem, because you can invoke the webmethods by hand in your browser. To diagnose it when it's someone else's service is a little trickier. You might be able to trace into the code for your web reference and inspect the text of the response before the exception is thrown.
I have found Fiddler to be highly useful in debugging http client server issues.
It is a proxy that allows you to intercept and even change the content of the request and response.
In your actual code, replace the line:
searchRequest.AppID = "APP ID you generated from ...";
with the actual AppID, which should be a long alpha-numeric sequence.