Click here to Skip to main content
15,882,209 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am getting exception "Remote name cannot be resolved" while invoking a request with http URL.

What I have tried:

My code is as below:
string result = string.Empty;
               //build the request object
               HttpWebRequest request = (HttpWebRequest)WebRequest.Create(txtRequestURL.Text);



               if (chkRequestHeader.Checked)
               {

                   request.Headers.Add("x-api-key", "49b05a5d-36c6-4ed4-98bf-720c2a3414e4");
               }


               request.ContentType = @"application/json";
               request.Accept = @"application/json";
               request.Method = "POST";

               //write the input data (aka post) to a byte array
               byte[] requestBytes = new ASCIIEncoding().GetBytes(txtReq.Text);
               //get the request stream to write the post to
               Stream requestStream = request.GetRequestStream();
               //write the post to the request stream
               requestStream.Write(requestBytes, 0, requestBytes.Length);
               txtLog.Text += "test_http()-process 6 Reached" + Environment.NewLine;
               //build a response object to hold the response
               //submit the request by calling get response
               HttpWebResponse response = (HttpWebResponse)request.GetResponse();
               //get the response stream to read the response from
               Stream responseStream = response.GetResponseStream();
               //now read it out to our result
               using (StreamReader rdr = new StreamReader(responseStream))
               {
                   //set the result to the contents of the stream
                   txtResponse.Text = rdr.ReadToEnd();
               }
Posted
Updated 15-Dec-22 2:12am
v2
Comments
Chris Copeland 15-Dec-22 8:07am    
This is generally caused when DNS isn't able to resolve a domain name or host name to an IP address. For example, if the txtRequestURL.Text value contained "http://iamnotavaliddomain.com/" (which isn't an actual website) I would expect you'd get that same error.

1 solution

No, you will have to use the debugger to find out exactly what is in txtRequestURL.Text and then see if that URL can be referenced at all.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900