Home > Net >  Consult the content-type one small problem
Consult the content-type one small problem

Time:10-09

Postman can debug successful commissioning a web link, it has a built-in header

The content-type: multipart/form - data; A boundary=& lt; Calculated the when the request is sent>
The Content - Length: & lt; Calculated the when the request is sent>

And what did I use visual studio to debug, debugging is failure,
The content-type written application/x - WWW - form - urlencoded when at 500 error, write text/HTML and multipart/form - when the data is the same failure,
Then I tried the postman bring the content-type cancelled, reported the same mistake, so that come out is the problem that the content-type,
Baidu the multipart/form - the data must be a separate field boundary, and so I want to ask how to change the field type, and there's original RequestData type string, how change can easily add separator,

 public static string UrlRequest (string UrlAddress, string RequestMethod, string RequestData=https://bbs.csdn.net/topics/null) 
{
GC. Collect ();
HttpWebRequest request=(HttpWebRequest) WebRequest. Create (UrlAddress);
Request. The Method=RequestMethod;
CookieContainer cookie=new CookieContainer ();
Request. CookieContainer=cookies;
Request. The ContentType="application/x - WWW - form - urlencoded";
If (RequestData!=null)
{
//request. ContentLength=RequestData. Length;
Stream RequestStream=request. GetRequestStream ();
Byte [] bytes=Encoding. UTF8. GetBytes (RequestData);
RequestStream. Write (0 bytes, bytes. The Length).
RequestStream. Close ();
}
HttpWebResponse response=(HttpWebResponse) request. The method GetResponse ();
The string encoding=response. ContentEncoding;
If (encoding==null | | encoding. The Length & lt; 1)
{
Encoding="utf-8";
}
StreamReader reader=new StreamReader (response. GetResponseStream (), Encoding, GetEncoding (Encoding));
String retString=reader. ReadToEnd ();
Request. The Abort ();
The response. The Close ();
Return retString;
}

CodePudding user response:

Try using WebClient. UploadFile
https://docs.microsoft.com/zh-cn/dotnet/api/system.net.webclient.uploadfile? View=netframework 4.8
  •  Tags:  
  • C#
  • Related