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 7905149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T10:22:08+00:00 2026-06-03T10:22:08+00:00

I need to make an HTTP request to get back translated text. If I

  • 0

I need to make an HTTP request to get back translated text.
If I do it manually via Internet Explorer it’s fast; in a second or less I get the result.

But for some reason if I do it with HttpWebRequest it takes much longer.

Here is the code I’m trying to use. It dosent work well; I’m getting error 404 (Not Found) from the server.

Could someone please fix this code for me? I’m also not sure if the encoding they are using is good enough.

I have the key; just didn’t publish it here.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Web;
using System.IO;

namespace GoogleTranslateing
{
    public partial class Form1 : Form
    {
        string apiKey = "My Key";
        string sourceLanguage = "en";
        string targetLanguage = "de";
        string googleUrl;
        string textToTranslate = "hello world";

        public Form1()
        {
            InitializeComponent();

            googleUrl = "https://www.googleapis.com/language/translate/v2?key=" + apiKey + "&q=" + textToTranslate + "&source=" + sourceLanguage + "&target=" + targetLanguage;

            webRequest();

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void webRequest()
        {
            // Create a request using a URL that can receive a post. 
            WebRequest request = WebRequest.Create(googleUrl);
            // Set the Method property of the request to POST.
            request.Method = "POST";
            // Create POST data and convert it to a byte array.
            string postData = textToTranslate;
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            // Set the ContentType property of the WebRequest.
            request.ContentType = "application/x-www-form-urlencoded";
            // Set the ContentLength property of the WebRequest.
            request.ContentLength = byteArray.Length;
            // Get the request stream.
            Stream dataStream = request.GetRequestStream();
            // Write the data to the request stream.
            dataStream.Write(byteArray, 0, byteArray.Length);
            // Close the Stream object.
            dataStream.Close();
            // Get the response.
            WebResponse response = request.GetResponse();
            // Display the status.
            Console.WriteLine(((HttpWebResponse)response).StatusDescription);
            // Get the stream containing content returned by the server.
            dataStream = response.GetResponseStream();
            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader(dataStream);
            // Read the content.
            string responseFromServer = reader.ReadToEnd();
            // Display the content.
            Console.WriteLine(responseFromServer);
            // Clean up the streams.
            reader.Close();
            dataStream.Close();
            response.Close();
        }
    }
}

And is there any faster way then using WebRequest and WebResponse?

  • 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-03T10:22:09+00:00Added an answer on June 3, 2026 at 10:22 am

    The 404 error is being generated because you’re trying to send POST data to a service that doesn’t allow it (and doesn’t require it either). Change your webRequest() to the following…

    private void webRequest()
    {
        // Create a request using a URL that can receive a post. 
        WebRequest request = WebRequest.Create(googleUrl);
        // Set the Method property of the request to POST^H^H^H^HGET.
        request.Method = "GET"; // <-- ** You're putting textToTranslate into the query string so there's no need to use POST. **
    
        //// Create POST data and convert it to a byte array.
        //string postData = textToTranslate;
        //byte[] byteArray = Encoding.UTF8.GetBytes(postData);
    
        // Set the ContentType property of the WebRequest.
        request.ContentType = "application/x-www-form-urlencoded";
    
        // ** Commenting out the bit that writes the post data to the request stream **
    
        //// Set the ContentLength property of the WebRequest.
        //request.ContentLength = byteArray.Length;
        //// Get the request stream.
        //Stream dataStream = request.GetRequestStream();
        //// Write the data to the request stream.
        //dataStream.Write(byteArray, 0, byteArray.Length);
        //// Close the Stream object.
        //dataStream.Close();
    
        // Get the response.
        WebResponse response = request.GetResponse();
        // Display the status.
        Console.WriteLine(((HttpWebResponse)response).StatusDescription);
        // Get the stream containing content returned by the server.
        Stream dataStream = response.GetResponseStream();
        // Open the stream using a StreamReader for easy access.
        StreamReader reader = new StreamReader(dataStream);
        // Read the content.
        string responseFromServer = reader.ReadToEnd();
        // Display the content.
        Console.WriteLine(responseFromServer);
        // Clean up the streams.
        reader.Close();
        dataStream.Close();
        response.Close();
    }
    

    I haven’t enabled billing on my Google API account; now I’m getting a (403) Forbidden error so I can’t verify that this is a complete fix, but give it a try. At least this gets around the 404 error issue.

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

Sidebar

Related Questions

I am trying to make a simple request using http.get. But I need to
I need to set time out for the Http Request we make to a
I would like to make an async GET request that returns back a document
I'm trying to make an HTTP GET request using the jQuery get() function, but
I would like to make a http get request with my Android phone (i
I need to send a HTTP request (and get XML response) from Flash that
I need to make an HTTP POST request from outside the browser, but the
I need to use a GET request to send JSON to my server via
Hey, I need to make a HTTP POST request with an array of NSDictionary
I need to make this design: http://www.stephburningham.com/lmg/ into a joomla template but I've never

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.