///////////////////////////////////////////////////////////////////////////////////////////
//add Post parameter
System. Text. StringBuilder builder=new System. Text. StringBuilder ();
Int I=0;
The foreach (var item in dic)
{
If (I & gt; 0)
Builder. Append (" & amp;" );
Builder. AppendFormat (="{0} {1}", the item. The Key of the item. The Value).
i++;
}
Byte [] postData=https://bbs.csdn.net/topics/System.Text.Encoding.UTF8.GetBytes (builder. The ToString ());
System.Net.HttpWebRequest _webRequest=(System.Net.HttpWebRequest) System.Net.WebRequest.Create (url);
_webRequest. Method="POST";
//HttpWebRequest HTTP=(HttpWebRequest) WebRequest. Create (url);
//- H need to have a Header in the HTTP request information
//- H "Accept: application/json"
_webRequest. Accept="application/json";
_webRequest. Headers. The Add (" username ", "13332999209,");
_webRequest. Headers. The Add (" password ", "123456");
://-u account password, submit the authentication information
//_webRequest. Headers. The Add (" Authorization ", "Basic" + Convert. ToBase64String (... Syste. Text Encoding UTF8 GetBytes (" password "account:)));
//_webRequest ContentType="application/json; charset=UTF-8";
//content type
_webRequest. ContentType="application/x - WWW - form - urlencoded";
//_webRequest ContentType="text/plain";
_webRequest. Timeout=1000 * 30;
_webRequest. Proxy=null;
_webRequest. ContentLength=postData. Length;
Using (System. IO Stream reqStream=_webRequest. GetRequestStream ())
{
ReqStream. Write (postData, 0, postData. Length);
ReqStream. Close ();
}
System.Net.HttpWebResponse resp=(System.Net.HttpWebResponse) _webRequest. The method GetResponse ();//logon failure here, return 400
System. IO. Stream Stream=resp. GetResponseStream ();
//get response content
Using (System. IO StreamReader reader=new System. IO. StreamReader (stream, System. Text. Encoding. UTF8))
{
Result=reader. ReadToEnd ();
}
return result;
CodePudding user response:
Is that your data is wrong, the json data can't write like that, you'd better use other json toolkits dic object into a json stringHere recommended Postman test first and then contrast to write your code
The Postman used is introduced: https://www.jianshu.com/p/8f8014ad85aa
CodePudding user response:
In general I write is request body in the form of a Json string, and then use these two functions to handlePublic static HttpWebResponse HttpResponsePost (url string, the string postData=https://bbs.csdn.net/topics/null,
Encoding requestEncoding=null, int? Timeout=null string userAgent=null,
CookieCollection cookies=null, bool sendJson=false, bool sendXml=false)
{
If (string. IsNullOrEmpty (url))
{
Throw new ArgumentNullException (" url ");
}
If (requestEncoding==null)
{
RequestEncoding.=Encoding UTF8;
}
HttpWebRequest request=null;
//if it is send HTTPS requests
If (url. StartsWith (" HTTPS, "StringComparison. OrdinalIgnoreCase))
{
ServicePointManager. ServerCertificateValidationCallback=
New RemoteCertificateValidationCallback (CheckValidationResult);
request=WebRequest.Create(url) as HttpWebRequest;
request.ProtocolVersion=HttpVersion.Version10;
}
The else
{
request=WebRequest.Create(url) as HttpWebRequest;
}
request.Method="POST";
If (sendJson)
{
Request. ContentType="application/json";
}
Else if (sendXml)
{
Request. ContentType="application/XML";
}
The else
{
Request. The ContentType="application/x - WWW - form - urlencoded";
}
if (! String. IsNullOrEmpty (userAgent))
{
Request. UserAgent=UserAgent;
}
The else
{
request.UserAgent=DefaultUserAgent;
}
If (a timeout. HasValue)
{
Value; request. A Timeout=a Timeout.
}
If (cookies!=null)
{
Request. CookieContainer=new CookieContainer ();
Request. CookieContainer. Add (cookies);
}
//if you need to POST data
if (! String. IsNullOrEmpty (postData))
{
Byte [] data=https://bbs.csdn.net/topics/requestEncoding.GetBytes (postData);
Using (Stream Stream=request. GetRequestStream ())
{
Stream. Write (data, 0, the data Length);
}
}
The return request. The method GetResponse () as HttpWebResponse;
}
CodePudding user response:
400 is returned by the server code, said can't find the corresponding goal url parsing,