0

I'm trying to open a connection to a server and get the response body, which is an image. I want to save it to Image, and later display it in the PictureBox. Here is my code:

try
    {
        var response = WebRequest.Create(String.Format(url + "?t=webwx&_={0}", timestamp));
        var stream = response.GetRequestStream();
        Image image = Image.FromStream(stream);
        qe_img.Image = image;
        qe_img.Height = image.Height;
        qe_img.Width = image.Width;
     } catch (Exception e)
     {
        Console.WriteLine(e);
     }

I get:

Exception thrown: 'System.Net.ProtocolViolationException' in System.dll
System.Net.ProtocolViolationException: Cannot send a content-body with this verb-type.
   at System.Net.HttpWebRequest.CheckProtocol(Boolean onRequestStream)
   at System.Net.HttpWebRequest.GetRequestStream(TransportContext& context)
   at System.Net.HttpWebRequest.GetRequestStream()
   at WindowsFormsApplication1.Form1.showQRImage() in c:\users\morgan\documents\visual studio 2015\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs:line 70

But I always get a WebException. I'm new to C#, I wonder what is wrong here. Thanks.

5
  • 1
    Wrap your code in a try--catch block and paste the exception here. And also instead of using response.ResponseStream you need to call response.GetResponseStream() Commented Jan 6, 2016 at 6:06
  • Try getting Http code from exception example:stackoverflow.com/questions/3614034/… Commented Jan 6, 2016 at 6:09
  • @qamar Please see the edited code. Commented Jan 6, 2016 at 6:25
  • Try putting url from your Create() method into a IE and see if it works manually. I suspect the request is returning more than just the image. You can vailidate this by looking at the webpage returned in the IE by using menu : Source. You will need to find the tag that contains the image and extract the image from the html returned results. Commented Jan 6, 2016 at 6:36
  • I think you url missing something or inalid. Can you paste the value of Url variable here? Commented Jan 6, 2016 at 8:16

1 Answer 1

2

Try this

 try
            {
                var request = WebRequest.Create(String.Format(url + "?t=webwx&_={0}", timestamp));

                using (WebResponse response = request.GetResponse())
                {
                    using (Stream stream = response.GetResponseStream())
                    {
                        Image image = Image.FromStream(stream);
                        qe_img.Image = image;
                        qe_img.Height = image.Height;
                        qe_img.Width = image.Width;


                    }
                }

            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.