标签云

微信群

扫码加入我们

WeChat QR Code

Here's the code I'm using:

// create a request
HttpWebRequest request = (HttpWebRequest)
WebRequest.Create(url); request.KeepAlive = false;
request.ProtocolVersion = HttpVersion.Version10;
request.Method = "POST";


// turn our request string into a byte stream
byte[] postBytes = Encoding.UTF8.GetBytes(json);

// this is important - make sure you specify type this way
request.ContentType = "application/json; charset=UTF-8";
request.Accept = "application/json";
request.ContentLength = postBytes.Length;
request.CookieContainer = Cookies;
request.UserAgent = currentUserAgent;
Stream requestStream = request.GetRequestStream();

// now send it
requestStream.Write(postBytes, 0, postBytes.Length);
requestStream.Close();

// grab te response and print it out to the console along with the status code
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string result;
using (StreamReader rdr = new StreamReader(response.GetResponseStream()))
{
    result = rdr.ReadToEnd();
}

return result;

When I'm running this, I'm always getting 500 internal server error.

What am I doing wrong?


First, make sure that the data you post is what the server expects.

2018年07月18日06分03秒

actually, it looks like I was posting invalid data...

2018年07月18日06分03秒

For ease of work you can add json library to your visual studio too

2018年07月17日06分03秒

I think the json string line should be: string json = "{\"user\":\"test\"," + "\"password\":\"bla\"}"; It looks like you are missing a \

2018年07月17日06分03秒

Always use "application/json" (unless for some other reason text/json is needed for example: entwicklungsgedanken.de/2008/06/06/…). Creding goes to: stackoverflow.com/questions/477816/….

2018年07月17日06分03秒

I would've thought the streamWriter.Flush(); and streamWriter.Close(); is not necessary as you are inside a using block. At the end of the using block, stream writer will close anyway.

2018年07月18日06分03秒

Does this also work for HTTPS?

2018年07月18日06分03秒

user3772108 See stackoverflow.com/a/16380064/2279059. Use a JSON library, such as Newtonsoft JSON.Net, and render the JSON string from an object, or use serialization. I understand that this was omitted here for simplicity (although the simplicity gain is minimal), but formatting structured data strings (JSON, XML, ...) is too dangerous to do it even in trivial scenarios and to encourage people to copy such code.

2018年07月18日06分03秒

What's the difference between this and the above code, am I missing something?

2018年07月18日06分03秒

This uses the JavaScriptSerializer's Serialize method to create valid JSON instead of hand-crafting it.

2018年07月18日06分03秒

See Jean F's answer below - should be a comment. Take care with the content type application/json is correct.

2018年07月17日06分03秒

SeanAnderson I keep having "Unable to connect to the remote server" Error.

2018年07月17日06分03秒

I wonder why JavaScriptSerializer is not working on Unity3D. I used using System.Web.Script.Serialization directive, but namespace Web does not exists.

2018年07月18日06分03秒

now this answer and Sean Anderson's answer are exactly the same, as Sean has edited his post.

2018年07月17日06分03秒

Thanks for posting this solution Vivek. In our scenario we tried another solution in this post, and wound up seeing System.Threading exceptions in our application, due to what I assume were synchronous posts blocking threads. Your code solved our problem.

2018年07月17日06分03秒

This option is no longer available since .Net 4.5.2. see here stackoverflow.com/a/40525794/2161568

2018年07月18日06分03秒

Downvote per the above comment - since this isn't available, should probably remove the answer.

2018年07月17日06分03秒

Helpful, however PostAsJsonAsync is no longer available since .NET 4.5.2. Use PostAsync instead. More here

2018年07月18日06分03秒

sounds like a pretty bad idea, am i missing something?

2018年07月17日06分03秒