1

I am trying to post request via UnityWebRequest.Post method but got this error

"Curl error 1: Received HTTP/0.9 when not allowed".

Below mentioned is my code:

WWWForm form = new WWWForm();
form.AddField("username", userName);
form.AddField("password", password);

using (UnityWebRequest webRequest = UnityWebRequest.Post("http://193.16.117.212:48080/login", form))
{
    yield return webRequest.SendWebRequest();

    if (webRequest.result != UnityWebRequest.Result.Success)
    {
        Debug.Log(webRequest.error);
    }
    else
    {
        Debug.Log("Form upload complete");
    }
}

Can someone please help?

Asmar Ali
  • 35
  • 6
  • Is this server owned by you? The url "http://193.16.117.212:48080login" looks invalid, are you missing a `/` after the port? – shingo Jun 14 '22 at 10:50
  • http://193.16.117.212:48080/login that is the correct URL. Yes, the url is from the internal server. – Asmar Ali Jun 14 '22 at 10:52
  • The problem is on the server. Can you tell me what server are you using? Can you run `curl -v http://193.16.117.212:48080/login` to see what it will return? – shingo Jun 14 '22 at 11:00
  • @shingo it is returning this: curl -vhttp://193.16.117.212:48080/login Usage: curl [options...] -d, --data HTTP POST data -f, --fail Fail silently (no output at all) on HTTP errors -h, --help Get help for commands -i, --include Include protocol response headers in the output -o, --output Write to file instead of stdout -O, --remote-name Write output to a file named as the remote file -s, --silent Silent mode -T, --upload-file Transfer local FILE to destination – Asmar Ali Jun 14 '22 at 11:07
  • @shingo answer continued: -u, --user Server user and password -A, --user-agent Send User-Agent to server -v, --verbose Make the operation more talkative -V, --version Show version number and quit This is not the full help, this menu is stripped into categories. Use "--help category" to get an overview of all categories. For all options use the manual or "--help all". – Asmar Ali Jun 14 '22 at 11:07
  • There is a space after `-v` – shingo Jun 14 '22 at 11:09
  • @shingo it returned this: * Trying 193.16.117.212:48080... * Connected to 193.16.117.212 (193.16.117.212) port 48080 (#0) > GET /login HTTP/1.1 > Host: 193.16.117.212:48080 > User-Agent: curl/7.79.1 > Accept: */* > * Received HTTP/0.9 when not allowed * Closing connection 0 curl: (1) Received HTTP/0.9 when not allowed – Asmar Ali Jun 14 '22 at 11:12
  • According to the messages, the service at the address 193.16.117.212:48080 doesn't look like a HTTP service because it returns nothing for the request. Are you sure you have configured it? Can you access it with a browser? – shingo Jun 14 '22 at 11:19
  • @shingo it has been developed by one of my colleagues. He could configure it with the code mentioned below written in C#. static async Task Main(string[] args) { AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true); HttpClient myHttpClient = new HttpClient(new SocketsHttpHandler()) { DefaultRequestVersion = HttpVersion.Version20, DefaultVersionPolicy = HttpVersionPolicy.RequestVersionExact }; – Asmar Ali Jun 14 '22 at 11:23
  • myHttpClient.BaseAddress = new Uri("http://193.16.117.212:48080"); string requestUrl = "/login"; var parameters = new Dictionary { { "username", "kiaaa2" }, { "password", "kiaaa2" } }; var encodedContent = new FormUrlEncodedContent (parameters); Console.WriteLine($"GET {requestUrl}."); HttpResponseMessage response = await myHttpClient.PostAsync(requestUrl, encodedContent); string responseBody = await response.Content.ReadAsStringAsync(); Console.WriteLine($"Response Body Length is: {responseBody.Length}"); – Asmar Ali Jun 14 '22 at 11:23
  • @shingo The problem is I could not implement the same code inside unity as SocketsHttpHandler class is not recogniseable. Moreover, the server is only configured with using http2. I do not know if UnityWebRequest supports http2 or not. – Asmar Ali Jun 14 '22 at 11:24
  • It doesn't support http/2. You may try HttpClientHandler with HttpClient. – shingo Jun 14 '22 at 11:54
  • @shingo Can you please give an example, how? – Asmar Ali Jun 14 '22 at 12:26
  • @shingo Thanks. But with HTTPClientHandler how can I set the version to http2. Can you please reply with the syntax? I am sorry I am not a very expert into it. – Asmar Ali Jun 14 '22 at 12:46
  • It seems like you don't have to specify the version. A default `new HttpClient();` will just work. – shingo Jun 14 '22 at 12:53
  • @shingo A default HttpClient has sync methods which seem to not work with unity. I have tried implementing them using Result property which makes the operation asynchronous, but it did not work. – Asmar Ali Jun 14 '22 at 13:42
  • What platform is your unity project running? Now I know is [http/2 is supported on Windows 10](https://learn.microsoft.com/en-us/dotnet/framework/whats-new/), and you can also try this nuget package: [System.Net.Http.WinHttpHandler](https://www.nuget.org/packages/System.Net.Http.WinHttpHandler/) – shingo Jun 14 '22 at 15:19
  • @shingo I am using Windows 10 Education Edition. Let me try the package you mentioned. – Asmar Ali Jun 14 '22 at 16:24

0 Answers0