Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9267543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:34:30+00:00 2026-06-18T14:34:30+00:00

I have been trying to get HTTPRequest working in my C# Project for a

  • 0

I have been trying to get HTTPRequest working in my C# Project for a GET request, and I can not quite get it to work. Below is my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;
using System.Windows;
using System.Diagnostics;
using System.Threading;

class MyClass 
{
    const string URL_PREFIX = "http://mycompany.com/";
    private HttpWebRequest objRequest = null;
    private static string myRequestData = string.Empty;
    private string urlAddress;


    public MyClass()
    {
        int member = 1;
        int startLoc = 1;
        int endLoc = 1;
        string starttime = "2012-01-01 00:00:00";
        string endtime = "2012-01-01 00:00:00";
        int rt = 1;
        string cmt = "Hello World";

        this.urlAddress = URL_PREFIX + string.Format(
        "createtrip.php?member={0}&startLoc={1}&endLoc={2}&starttime={3}&endtime={4}&rt={5}&cmt={6}"
        , member, startLoc, endLoc, starttime, endtime, rt, cmt);

        StringBuilder completeUrl = new StringBuilder(urlAddress);
        objRequest = (HttpWebRequest)WebRequest.Create(urlAddress);
        objRequest.ContentType = "application/x-www-form-urlencoded";

        objRequest.BeginGetRequestStream(new AsyncCallback(httpComplete), objRequest);
    }
    private static void httpComplete(IAsyncResult asyncResult)
    {
        HttpWebRequest objHttpWebRequest = (HttpWebRequest)asyncResult.AsyncState;
        // End the operation
        Stream postStream = objHttpWebRequest.EndGetRequestStream(asyncResult);
        // Convert the string into a byte array.
        byte[] byteArray = Encoding.UTF8.GetBytes(myRequestData);
        // Write to the request stream.
        postStream.Write(byteArray, 0, myRequestData.Length);
        postStream.Close();

        // Start the asynchronous operation to get the response
        objHttpWebRequest.BeginGetResponse(new AsyncCallback(GetResponseCallback), objHttpWebRequest);

    }
    private static void GetResponseCallback(IAsyncResult asyncResult)
    {
        HttpWebRequest objHttpWebRequest = (HttpWebRequest)asyncResult.AsyncState;
        HttpWebResponse objHttpWebResponse = (HttpWebResponse)objHttpWebRequest.EndGetResponse(asyncResult);
        Stream objStreamResponse = objHttpWebResponse .GetResponseStream();
        StreamReader objStreamReader = new StreamReader(objStreamResponse );
        string responseString = objStreamReader.ReadToEnd();            // Got response here
         MessageBox.Show("RESPONSE :" + responseString);
        // Close the stream object
        objStreamResponse .Close();
        objStreamReader.Close();
        objHttpWebResponse.Close();
    }

}

The error I am currently getting is:

An exception of type 'System.Collections.Generic.KeyNotFoundException' occurred in mscorlib.ni.dll and wasn't handled before a managed/native boundary
A first chance exception of type 'System.Net.ProtocolViolationException' occurred in System.Windows.ni.dll
Operation is not valid due to the current state of the object.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T14:34:31+00:00Added an answer on June 18, 2026 at 2:34 pm

    http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.begingetrequeststream.aspx

    You cannot use the method “BeginGetRequestStream” with a GET or HEAD request (GET is the default one and the one you’re doing in the first HTTP request).

    Change to use “BeginGetResponse” as you’ve already done in the second part of the code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to get it to work using this thread but can't
I have been trying to get autocomplete to work using jquery. jsFiddle of example
I have been trying to get this thing to work for quite a while
Have been trying to get integration testing working with my seam project and the
I have been trying to get xapian working django haystack for a project im
I have been trying to get this code to work for a while now
I have been trying to get a simple xhr request to work but for
I have been trying to get a handle on using MVC 4.0 with EF
I have been trying to get Twitter Bootstrap btn-group with dropdown to work for
I have been trying to get nServiceBus to work with Ninject 2.0 as the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.