Skip to content

Commit

Permalink
Merge pull request #53 from Code501/master
Browse files Browse the repository at this point in the history
Fix 适配NSURLSession下post请求无法抓到POST请求参数问题
  • Loading branch information
coderyi authored Sep 25, 2019
2 parents c19857f + 417ca4c commit ce0da61
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 81 deletions.
71 changes: 5 additions & 66 deletions NetworkEye.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -238,15 +238,11 @@
isa = PBXNativeTarget;
buildConfigurationList = 0A4EF86D1BEB66FF000D2D7E /* Build configuration list for PBXNativeTarget "NetworkEye" */;
buildPhases = (
A770EAC8FB7D5B8B9B0CE23A /* 📦 Check Pods Manifest.lock */,
1E47BF57E219846EA68141A1 /* [CP] Check Pods Manifest.lock */,
A770EAC8FB7D5B8B9B0CE23A /* 📦 Check Pods Manifest.lock */,
0A4EF83C1BEB66FF000D2D7E /* Sources */,
0A4EF83D1BEB66FF000D2D7E /* Frameworks */,
0A4EF83E1BEB66FF000D2D7E /* Resources */,
9C3CAAA67AB0C2C3431F3247 /* [CP] Embed Pods Frameworks */,
7B2F435A39B78D9EC03D90A8 /* [CP] Copy Pods Resources */,
84F18E0EC91F6329F079C6D3 /* 📦 Embed Pods Frameworks */,
B04476CE907239E9F7890B3B /* 📦 Copy Pods Resources */,
);
buildRules = (
);
Expand Down Expand Up @@ -370,58 +366,16 @@
files = (
);
inputPaths = (
"${PODS_PODFILE_DIR_PATH}/Podfile.lock",
"${PODS_ROOT}/Manifest.lock",
);
name = "[CP] Check Pods Manifest.lock";
outputPaths = (
"$(DERIVED_FILE_DIR)/Pods-NetworkEye-checkManifestLockResult.txt",
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [[ $? != 0 ]] ; then\n cat << EOM\nerror: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\nEOM\n exit 1\nfi\n";
showEnvVarsInLog = 0;
};
7B2F435A39B78D9EC03D90A8 /* [CP] Copy Pods Resources */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
);
name = "[CP] Copy Pods Resources";
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-NetworkEye/Pods-NetworkEye-resources.sh\"\n";
showEnvVarsInLog = 0;
};
84F18E0EC91F6329F079C6D3 /* 📦 Embed Pods Frameworks */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
);
name = "📦 Embed Pods Frameworks";
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-NetworkEye/Pods-NetworkEye-frameworks.sh\"\n";
showEnvVarsInLog = 0;
};
9C3CAAA67AB0C2C3431F3247 /* [CP] Embed Pods Frameworks */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
);
name = "[CP] Embed Pods Frameworks";
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-NetworkEye/Pods-NetworkEye-frameworks.sh\"\n";
shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n# This output is used by Xcode 'outputs' to avoid re-running this script phase.\necho \"SUCCESS\" > \"${SCRIPT_OUTPUT_FILE_0}\"\n";
showEnvVarsInLog = 0;
};
A770EAC8FB7D5B8B9B0CE23A /* 📦 Check Pods Manifest.lock */ = {
Expand All @@ -439,21 +393,6 @@
shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [[ $? != 0 ]] ; then\n cat << EOM\nerror: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\nEOM\n exit 1\nfi\n";
showEnvVarsInLog = 0;
};
B04476CE907239E9F7890B3B /* 📦 Copy Pods Resources */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
);
name = "📦 Copy Pods Resources";
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-NetworkEye/Pods-NetworkEye-resources.sh\"\n";
showEnvVarsInLog = 0;
};
/* End PBXShellScriptBuildPhase section */

/* Begin PBXSourcesBuildPhase section */
Expand Down
8 changes: 8 additions & 0 deletions NetworkEye.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
16 changes: 10 additions & 6 deletions NetworkEye/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="6211" systemVersion="14A298i" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="BYZ-38-t0r">
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14868" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<device id="retina6_1" orientation="portrait" appearance="light"/>
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6204"/>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14824"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--View Controller-->
<scene sceneID="tne-QT-ifu">
<objects>
<viewController id="BYZ-38-t0r" customClass="ViewController" customModuleProvider="" sceneMemberID="viewController">
<viewController id="BYZ-38-t0r" customClass="ViewController" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="y3c-jy-aDJ"/>
<viewControllerLayoutGuide type="bottom" id="wfy-db-euE"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</view>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="26" y="29"/>
</scene>
</scenes>
</document>
60 changes: 57 additions & 3 deletions NetworkEye/DemoViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,29 @@ - (void)setupView{
btSessionRequest.titleLabel.font=[UIFont systemFontOfSize:15];




UIWebView *webView=[[UIWebView alloc] initWithFrame:CGRectMake(0, 300, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height-64-330)];
UIButton *btURLConnectionOfPostRequest=[UIButton buttonWithType:UIButtonTypeCustom];
[self.view addSubview:btURLConnectionOfPostRequest];
[btURLConnectionOfPostRequest addTarget:self action:@selector(btURLConnectionOfPOSTRequestAction) forControlEvents:UIControlEventTouchUpInside];
btURLConnectionOfPostRequest.frame=CGRectMake(([[UIScreen mainScreen] bounds].size.width-300)/2, 280, 300, 35);
[btURLConnectionOfPostRequest setTitle:@"Click to NSURLConnection Post request" forState:UIControlStateNormal];
[btURLConnectionOfPostRequest setTitleColor:[UIColor colorWithRed:0.24f green:0.51f blue:0.78f alpha:1.00f] forState:UIControlStateNormal];
btURLConnectionOfPostRequest.layer.borderColor=[UIColor colorWithRed:0.24f green:0.51f blue:0.78f alpha:1.00f].CGColor;
btURLConnectionOfPostRequest.layer.borderWidth=0.4;
btURLConnectionOfPostRequest.titleLabel.font=[UIFont systemFontOfSize:15];


UIButton *btNSURLSessionOfPostRequest=[UIButton buttonWithType:UIButtonTypeCustom];
[self.view addSubview:btNSURLSessionOfPostRequest];
[btNSURLSessionOfPostRequest addTarget:self action:@selector(btNSURLSessionOfPOSTRequestAction) forControlEvents:UIControlEventTouchUpInside];
btNSURLSessionOfPostRequest.frame=CGRectMake(([[UIScreen mainScreen] bounds].size.width-300)/2, 310, 300, 35);
[btNSURLSessionOfPostRequest setTitle:@"Click to NSURLSession Post request" forState:UIControlStateNormal];
[btNSURLSessionOfPostRequest setTitleColor:[UIColor colorWithRed:0.24f green:0.51f blue:0.78f alpha:1.00f] forState:UIControlStateNormal];
btNSURLSessionOfPostRequest.layer.borderColor=[UIColor colorWithRed:0.24f green:0.51f blue:0.78f alpha:1.00f].CGColor;
btNSURLSessionOfPostRequest.layer.borderWidth=0.4;
btNSURLSessionOfPostRequest.titleLabel.font=[UIFont systemFontOfSize:15];


UIWebView *webView=[[UIWebView alloc] initWithFrame:CGRectMake(0, 360, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height-64-360)];
[self.view addSubview:webView];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.github.com"]]];

Expand Down Expand Up @@ -149,6 +169,40 @@ - (void)btSessionRequestAction{

}

- (void)btURLConnectionOfPOSTRequestAction{

NSURL *url = [NSURL URLWithString:@"http://www.example.com"];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.timeoutInterval = 10;
request.HTTPMethod = @"POST";

request.HTTPBody = [@"name=NetworkEye&password=123456" dataUsingEncoding:NSUTF8StringEncoding];

NSOperationQueue *queue = [NSOperationQueue mainQueue];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSLog(@"返回数据-%@",data);
}];
}

- (void)btNSURLSessionOfPOSTRequestAction{
NSURL *url = [NSURL URLWithString:@"http://www.example.com"];
NSMutableURLRequest *mutableRequest = [NSMutableURLRequest requestWithURL:url];
NSString *bobyString = @"name=NetworkEye&password=123456";

NSData *postData = [bobyString dataUsingEncoding:NSUTF8StringEncoding];
[mutableRequest setHTTPMethod:@"POST"];
[mutableRequest setHTTPBody:postData];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionTask *task = [session dataTaskWithRequest:mutableRequest completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (error == nil) {
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSLog(@"dict = %@", dict);
}

}];
[task resume];
}

- (void)testRequest {

Expand Down
17 changes: 16 additions & 1 deletion NetworkEye/NetworkEye/NEHTTPModel.m
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,22 @@ -(void)setNe_request:(NSURLRequest *)ne_request_new{
if ([ne_request HTTPBody].length>512) {
self.requestHTTPBody=@"requestHTTPBody too long";
}else{
self.requestHTTPBody=[[NSString alloc] initWithData:[ne_request HTTPBody] encoding:NSUTF8StringEncoding];
if ([ne_request.HTTPMethod isEqualToString:@"POST"] && !ne_request.HTTPBody) {
uint8_t rd[1024] = {0};
NSInputStream *stream = ne_request.HTTPBodyStream;
NSMutableData *data = [[NSMutableData alloc] init];
[stream open];
while ([stream hasBytesAvailable]) {
NSInteger len = [stream read:rd maxLength:1024];
if (len > 0 && stream.streamError == nil) {
[data appendBytes:(void *)rd length:len];
}
}
self.requestHTTPBody=[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
[stream close];
}else{
self.requestHTTPBody=[[NSString alloc] initWithData:[ne_request HTTPBody] encoding:NSUTF8StringEncoding];
}
}
if (self.requestHTTPBody.length>1) {
if ([[self.requestHTTPBody substringFromIndex:self.requestHTTPBody.length-1] isEqualToString:@"\n"]) {
Expand Down
13 changes: 8 additions & 5 deletions Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
PODS:
- FMDB/common (2.5)
- FMDB/SQLCipher (2.5):
- FMDB/common
- FMDB/SQLCipher (2.7.5):
- SQLCipher
- SQLCipher (3.1.0):
- SQLCipher/standard (= 3.1.0)
Expand All @@ -12,10 +10,15 @@ PODS:
DEPENDENCIES:
- FMDB/SQLCipher (~> 2.5)

SPEC REPOS:
https://github.com/cocoapods/specs.git:
- FMDB
- SQLCipher

SPEC CHECKSUMS:
FMDB: 96e8f1bcc1329e269330f99770ad4285d9003e52
FMDB: 2ce00b547f966261cd18927a3ddb07cb6f3db82a
SQLCipher: 4684434d5791db86512cd7e02172aefba575849e

PODFILE CHECKSUM: 9763126a952e181b99dc45f49b3235935404f17b

COCOAPODS: 1.0.0
COCOAPODS: 1.7.5

0 comments on commit ce0da61

Please sign in to comment.